Responses
The Photodrop users controller returns JSON with 201 Created when the service creates a user. Choose the response body, status, and headers in the controller. Return the response helper's error so the HTTP adapter can handle a failed write.
Response Helpers
Use these web.Context methods in an HTTP handler. Each call below is an independent handler return, with net/http imported and user holding the service result.
return ctx.JSON(http.StatusCreated, user)
return ctx.Text(http.StatusOK, "ok")
return ctx.HTML(http.StatusOK, "<p>Upload received.</p>")
return ctx.Blob(http.StatusOK, "image/png", imageBytes)
return ctx.NoContent(http.StatusNoContent)JSON serializes a Go value. Text, HTML, and Blob set the corresponding content type. NoContent sends a status without a body, for example after a successful deletion. HTML writes an HTML string, so escape untrusted values or render them with html/template first.
Use Web for the package-level response contract. Render templates with your chosen Go template package before writing an HTML response.
JSON Shape
The create endpoint returns the same User fields as the lookup endpoint. Its success status is 201.
{"id":"user_123","name":"Ada Lovelace","email":"[email protected]"}Use a typed response once clients depend on its fields. If your API uses an envelope, give that envelope a type too. Keep credentials and internal persistence fields out of response types. HTTP Tests checks the successful body fields and status.
Headers and Cookies
Set headers before calling a response helper. In Store, set Location to the new user's lookup URL before returning the existing JSON response.
ctx.SetHeader("Location", "/api/v1/users/"+user.ID)
ctx.SetHeader("Cache-Control", "no-store")
return ctx.JSON(http.StatusCreated, user)SetHeader replaces the header value. AddHeader appends another value. Use SetCookie with net/http.Cookie for response cookies. This independent handler fragment records a display preference.
ctx.SetCookie(&http.Cookie{
Name: "photo_view",
Value: "grid",
Path: "/",
MaxAge: 86400,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})
return ctx.NoContent(http.StatusNoContent)This cookie requires HTTPS because Secure is true. To delete it, set an empty value and MaxAge: -1 with the same name, path, and domain. Use Auth for authentication sessions and their security policy.
Redirects
Return ctx.Redirect with an HTTP status and destination. After a browser form submission succeeds, use 303 See Other so the browser follows with a GET request.
return ctx.Redirect(http.StatusSeeOther, "/api/v1/users/"+user.ID)Use 302 Found for a temporary navigation redirect. Use 307 Temporary Redirect or 308 Permanent Redirect when clients must preserve the request method and body. Choose destinations in your application or validate external destinations before redirecting.
Files and Downloads
Use ctx.File(path) for a file on the server's filesystem. To make it a download, set Content-Disposition first. This independent handler fragment assumes an authorized, application-selected file at the shown path.
ctx.SetHeader("Content-Disposition", `attachment; filename="photos.csv"`)
return ctx.File("exports/photos.csv")The Echo adapter delegates File to http.ServeFile, including its missing-file and range-request behavior. Keep filesystem paths under application control. Use Storage Patterns when the file lives on a named storage disk instead of the local filesystem.
Streamed Responses
For a response you cannot hold in memory, use ctx.ResponseWriter() with standard net/http APIs. Set headers and finish authorization and input checks before writing. The first body write commits the response, so a later error cannot replace it with a JSON error response. Use http.ResponseController when you need to flush buffered output, and handle write or flush failures.
Streaming and file delivery still occupy the request's lifetime. Pass ctx.Context() to upstream work so disconnects and cancellation can stop it. Use a job for work that should outlive the request.
Error Mapping
Store returns 400 for an undecodable body and 422 for decoded fields that fail validation. Show maps ErrUserNotFound to 404 with {"error":"user not found"}. Both return unexpected service errors to the HTTP adapter. See Error Handling Basics for the mapping and default error response.
Local Error Detail
The generated HTTP middleware can log locally written error bodies. See Local Error Detail for its limits and production investigation tools.
Readiness Errors
Public readiness returns a summary. Use the diagnostic token with authorized readiness to see structured dependency checks. Readiness Errors explains this separate diagnostic contract.
Next Steps
Add HTTP route tests to verify status codes, headers, response fields, and the body limit together. Use Error Handling Basics to distinguish expected client errors from unexpected service failures.