APIs
How to Test a REST API from Your Browser
Testing an API used to mean installing a desktop client or wrestling with long curl commands. An HTTP client in the browser does the same job: build a request, send it, and read the status, headers and body. Here's a quick workflow, from a first GET to authenticated requests you can share as curl.
Send your first request
- Open the editor and the HTTP client from the activity bar.
- Choose a method (GET, POST, PUT, PATCH, DELETE, HEAD or OPTIONS) and enter the URL.
- Add query parameters in the Params tab, or type them into the URL.
- Send, then read the status code, response time and size, and the body and headers.
JSON responses are pretty-printed and highlighted, and you can open a response in an editor tab to search it, fold it, or compare it with another response in the diff view.
Status codes at a glance
| Code | Meaning | Usual cause |
|---|---|---|
| 200 / 201 / 204 | OK / Created / No content | Success |
| 301 / 302 / 304 | Moved / Found / Not modified | Redirects and caching |
| 400 | Bad request | Malformed body or missing field |
| 401 / 403 | Unauthorised / Forbidden | Missing or wrong credentials / not allowed |
| 404 | Not found | Wrong path or ID |
| 415 / 422 | Unsupported media type / Unprocessable | Wrong Content-Type / validation failed |
| 429 | Too many requests | Rate limited; wait and retry |
| 500 / 502 / 503 | Server error / Bad gateway / Unavailable | A problem on the server side |
Sending JSON and form data
For POST, PUT and PATCH, choose a body type: JSON, raw text, form-data, URL-encoded or GraphQL. With JSON the client sets Content-Type: application/json for you, which is the most common thing to forget when a server answers 415 or ignores the body.
{
"title": "Buy milk",
"done": false
}Authentication
- Bearer token: sends
Authorization: Bearer <token>, the usual choice for OAuth and JWT APIs. - Basic auth: a username and password, encoded into the
Authorizationheader. - API key: a named key sent as a header or as a query parameter, whichever the API documents.
Keep secrets out of the URL where you can: URLs end up in logs and browser history, headers usually don't.
Environment variables
Put values you reuse (a base URL, a token) in the Env tab and reference them as {{NAME}} anywhere in the URL, headers or body. Switching from staging to production is then one value, not twenty edits:
GET {{BASE_URL}}/v1/todos?limit=10
Authorization: Bearer {{TOKEN}}Import and export curl
API docs usually show examples as curl. Paste one into the curl import and the method, URL, headers and body are filled in. Going the other way, Copy as curl gives a command with your variables and auth already filled in, ready for a terminal or a bug report.
curl -X POST "https://api.example.com/v1/todos" \
-H "Content-Type: application/json" \
-d '{"title":"Buy milk","done":false}'CORS, redirects and timeouts
A request made by a web page's own JavaScript is subject to CORS, so many APIs refuse it. Code Editor's HTTP client sends requests through its server instead, so CORS doesn't block them, the same as a desktop client. For the same reason it refuses private and local addresses (localhost, 192.168.x.x), so use curl for APIs running on your own machine.
- Follow redirects is on by default; turn it off to inspect a 301 or 302 itself.
- The timeout is 30 seconds by default and can be set from 1 to 120.
- Recent requests are kept in a history so you can resend them.