api

go
package api

import (
  "encoding/json"
  "errors"
  "io"

Streaming JSON decoding with DisallowUnknownFields

go http json
by Leah Thompson 1 tab
ruby
module ApiErrorHandler
  extend ActiveSupport::Concern

  included do
    rescue_from StandardError, with: :handle_standard_error
    rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found

Structured JSON error responses

rails api error-handling
by Alex Kumar 1 tab
go
package types

import (
  "bytes"
  "encoding/json"
  "time"

Strict JSON time parsing with custom UnmarshalJSON

go json time
by Leah Thompson 1 tab
ruby
class AddOptimisticLockingToDocuments < ActiveRecord::Migration[7.1]
  def change
    add_column :documents, :lock_version, :integer, null: false, default: 0
    add_index :documents, :updated_at
  end
end

Optimistic Locking for Collaborative Edits

rails activerecord concurrency
by codesnips 3 tabs
php
<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

Laravel rate limiting for API protection

laravel rate-limiting api
by Carlos Mendez 2 tabs
ruby
module Idempotency
  extend ActiveSupport::Concern

  included do
    before_action :check_idempotency_key, only: [:create, :update]
    after_action :store_idempotent_response, only: [:create, :update]

Request deduplication with idempotency keys

rails api reliability
by Alex Kumar 1 tab
ruby
module Api
  module V1
    class PostsController < ApplicationController
      def create
        post = current_user.posts.build(post_params)

Rails strong parameters for nested attributes

rails security strong-parameters
by Maya Patel 1 tab
python
from rest_framework import permissions


class IsOwnerOrReadOnly(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.

Django REST Framework viewset with custom permissions

django python rest
by Priya Sharma 2 tabs
python
INSTALLED_APPS += ['corsheaders']

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',  # Must be before CommonMiddleware
    'django.middleware.common.CommonMiddleware',
    # ... other middleware

Django CORS configuration for API access

django python cors
by Priya Sharma 1 tab
go
package api

import (
  "encoding/json"
  "errors"
  "io"

Strict JSON decode helper (size limit + unknown fields)

go http json
by Leah Thompson 1 tab
ruby
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :authenticate_user!
end

CSRF protection for Rails and JSON APIs

csrf rails api
by Kai Nakamura 2 tabs
go
package api

import (
  "net/http"
  "strings"
)

Enforce JSON Content-Type and method early in handlers

go http api
by Leah Thompson 1 tab