Skip to content
OddsockLocal
Local
Source text

Formatting

1 wordsRuns in this browser. Nothing is uploaded.

How to use

  1. Enter an absolute http or https URL.
  2. Choose the method. For GET or HEAD, leave the body empty.
  3. Add headers as Name: value lines.
  4. Copy the snippet. It is not run.

What is fetch() snippet?

Fetch generator prints an await fetch() call and a console.log of the response text. A POST to https://example.test/api with Content-Type: application/json and body {"name":"Sam"} sets method, a lowercase content-type header, and the JSON as the body string. You copy it into a browser console or a module. The page itself does not call fetch.

Read more

GET and HEAD are refused if a body is filled in. The Fetch standard does not allow those methods to send a body, and a snippet that included one would fail later in a way that looks like a server bug. Other methods keep the body. Leave the body field empty for a plain GET and the property is simply absent.

Headers are one Name: value line each. A repeated name is an error, because fetch would have to fold them and this page will not guess how. The URL is absolute http or https with no userinfo. Snippet format is fetch and stays there.

Use cases

  • A frontend change needs a fetch call in a PR description, matching the headers the UI will send.
  • You are about to debug in the browser console and want the options object written out, body included.
  • A GET was accidentally given a JSON body, and you want that mistake caught before the snippet is shared.
  • The curl you had has already been converted, and now you want the fetch form built from the fields directly.

POST, with the JSON left as a string

URL https://example.test/api, method POST, header Content-Type: application/json, body {"name":"Sam"}.

const response = await fetch("https://example.test/api", { "method": "POST", "headers": { "content-type": "application/json" }, "body": "{\"name\":\"Sam\"}" }); console.log(await response.text());

Why use Oddsock

Refusing a body on GET saves the argument later, when the console says the request failed and nobody remembers the body was illegal.

Related tools

Using this tool

fetch rejects a body on GET and HEAD. The page refuses that combination so the snippet you copy is one the browser can actually send.