Skip to content

Commit

Permalink
docs: improve explanation about body option (#276)
Browse files Browse the repository at this point in the history
* docs: improve explanation about `body` option

* add note about buffer too + improves

* update

---------

Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
nozomuikuta and pi0 authored Aug 23, 2023
1 parent 6139785 commit 9a49992
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ await ofetch("/api/generate-image", { responseType: "blob" });

## ✔️ JSON Body

`ofetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` and `Accept` headers (for `put`, `patch` and `post` requests).
If an object or a class with a `.toJSON()` method is passed to the `body` option, `ofetch` automatically stringifies it.

`ofetch` utilizes `JSON.stringify()` to convert the passed object. Classes without a `.toJSON()` method have to be converted into a string value in advance before being passed to the `body` option.

For `PUT`, `PATCH`, and `POST` request methods, when a string or object body is set, `ofetch` adds the default `content-type: "application/json"` and `accept: "application/json"` headers (which you can always override).

Additionally, `ofetch` supports binary responses with `Buffer`, `ReadableStream`, `Stream`, and [compatible body types](https://developer.mozilla.org/en-US/docs/Web/API/fetch#body). ofetch will automatically set the `duplex: "half"` option for streaming support!

**Example:**

```js
const { users } = await ofetch("/api/users", {
Expand Down

0 comments on commit 9a49992

Please sign in to comment.