SIP

502 Bad Gateway レスポンスのモック

The server, acting as a gateway or proxy, received an invalid response from a downstream server it accessed in attempting to fulfill the request.

完全なリファレンスを見る →

1 HTTP レスポンス

HTTP レスポンス
HTTP/1.1 502 Bad Gateway
Content-Type: application/json
Date: Tue, 25 Feb 2026 12:00:00 GMT
Content-Length: 60

{
  "error": "bad_gateway",
  "message": "Upstream server error"
}

2 curl でテストする

terminal
curl -i https://httpbin.org/status/502

3 フレームワークで 502 Bad Gateway を返す

django
from django.http import JsonResponse


def my_view(request):
    return JsonResponse(
        {"error": "bad_gateway", "message": "Upstream server error"},
        status=502,
    )
flask
from flask import Flask, jsonify

app = Flask(__name__)


@app.route("/endpoint")
def my_endpoint():
    return jsonify({"error": "bad_gateway", "message": "Upstream server error"}), 502
fastapi
from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI()


@app.get("/endpoint")
def my_endpoint():
    return JSONResponse(
        content={"error": "bad_gateway", "message": "Upstream server error"},
        status_code=502,
    )
express.js
// Express.js
app.get('/endpoint', (req, res) => {
  res.status(502).json({"error": "bad_gateway", "message": "Upstream server error"});
});
spring boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
public class MyController {

    @GetMapping("/endpoint")
    public ResponseEntity<Map<String, Object>> myEndpoint() {
        return ResponseEntity
            .status(502)
            .body(Map.of("error", "bad_gateway",
                         "message", "Bad Gateway"));
    }
}
go net/http
package main

import (
    "encoding/json"
    "net/http"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(502)
    json.NewEncoder(w).Encode(map[string]string{
        "error":   "bad_gateway",
        "message": "Bad Gateway",
    })
}
ruby on rails
class MyController < ApplicationController
  def my_action
    render json: {"error": "bad_gateway", "message": "Upstream server error"},
           status: :bad_gateway
  end
end
asp.net core
// ASP.NET Core Minimal API
app.MapGet("/endpoint", () =>
    Results.Json(
        new { error = "bad_gateway", message = "Bad Gateway" },
        statusCode: 502
    )
);

他の SIP モックページ