Code snippets
Turn the request you are working on into code: a curl command for a terminal or a bug report, a JavaScript fetch call, a Python requests call, or a postino command that sends it from a terminal without opening the window.
Generating a snippet
- Open the request.
- Click Code, at the right of the request editor's tabs.
- Pick cURL, fetch, Python or Postino CLI, or press ← and →. The code is highlighted like the request editor's.
- Click Copy, or press Ctrl C (⌘ C on macOS). What is copied is exactly what the dialog shows, and the button says Copied for a moment. To copy only part of it, select it in the code first.
What goes into the snippet
The snippet is built from what is in the editor, saved or not, resolved against the active environment and the session variables:
- Variables and template functions are replaced. A template function such as
{{ uuid() }}is evaluated once for the snippet. - Enabled query parameters are appended to the URL, percent-encoded.
- Only enabled headers are included, plus the automatic
Content-Typefor JSON, XML and form bodies. - The Auth tab is included as the request sends it: the
Authorizationheader of Basic and Bearer, and the header or query parameter of an API key. - With OAuth 2.0, the snippet carries the token Postino holds for the active environment, in the header or query parameter of the profile. When there is none yet, or it has expired, the snippet shows
<access token>in its place: send the request once to get a token. The Code dialog never signs in. - A form body is written field by field, in the style of each language.
- Scripts are not run. Variables that only a pre-request script would set stay as
{{name}}in the snippet, and so does any unknown variable.
Because variables are resolved, the snippet contains the real values of tokens and passwords from your environment, including .local.env, and the OAuth 2.0 token of the session. Check it before pasting it into an issue or a chat.
From the command line
postino snippet <file> --lang curl|fetch|python|postino prints the same code in a terminal, and postino import curl goes the other way, from a curl command to a request. See the command line reference.
Examples
For this request, with baseUrl set to https://api.example.com and token to abc123:
POST {{baseUrl}}/users
Authorization: Bearer {{token}}
::: body json
{ "name": "Ada" }cURL
One option per line. -X is left out for GET. Values are single quoted for a POSIX shell. A form body becomes one --data-urlencode per field.
curl \
-X POST \
'https://api.example.com/users' \
-H 'Authorization: Bearer abc123' \
-H 'Content-Type: application/json' \
--data-raw '{ "name": "Ada" }'JavaScript fetch
A JSON object or array becomes an object literal inside JSON.stringify(...), and a form body new URLSearchParams({...}). A body that is not valid JSON, for example because it still has a {{name}} marker, stays a single string.
await fetch("https://api.example.com/users", {
method: "POST",
headers: {
"Authorization": "Bearer abc123",
"Content-Type": "application/json",
},
body: JSON.stringify({ name: "Ada" }),
});Python requests
A JSON object or array becomes a dict or list passed as json, with True, False and None. A form body becomes a dict passed as data, and any other body a string passed as data.
import requests
response = requests.request(
"POST",
"https://api.example.com/users",
headers={
"Authorization": "Bearer abc123",
"Content-Type": "application/json",
},
json={"name": "Ada"},
)Postino CLI
A one-off request of the postino command. A standard method is its own command, such as postino post, and any other goes through postino http METHOD. Every header is a -H, the Authorization header of the Auth tab included, so the command sends exactly what the request sends. A multipart body becomes one -F per field, with @file for files. Any other body, a form one included, is a single -d next to its Content-Type header; a body that starts with @ is piped in with -d @-, since -d @file reads a file.
postino post \
'https://api.example.com/users' \
-H 'Authorization: Bearer abc123' \
-H 'Content-Type: application/json' \
-d '{ "name": "Ada" }'The cURL and Postino CLI snippets use POSIX shell quoting. Run it in Git Bash or WSL, or adapt the quotes for PowerShell.