JSON Convenience
Use the json option to send a serialized JSON request body without manual stringification or headers.
The json option serializes the request body and sets Content-Type.
import { api } from "@zap-studio/fetch";
await api.post("/api/users", UserSchema, {
json: { name: "Ada" },
});
UserSchema still validates the response — the json value is only the outgoing request body and is not validated by the schema.
What It Sets
Providing json sets:
bodytoJSON.stringify(json)Content-Typetoapplication/json, when no content type is already set onheaders
import { $fetch } from "@zap-studio/fetch";
const user = await $fetch("https://api.example.com/users", UserSchema, {
method: "POST",
json: { name: "Ada" },
});
Mutually Exclusive with body
json and native body cannot be used together — providing both throws a TypeError.
await $fetch("/api/upload", {
method: "POST",
body: formData, // use native `body` for FormData, Blob, ReadableStream, etc.
});
See Also
- Validated Fetch Mode — how the response schema and
jsoninteract. - HTTP Method Helpers —
api.post,api.put, andapi.patchexamples. - Overview — package summary and quick start.