From 9a499926f573fa749c552a3f51d1259a9a18d3aa Mon Sep 17 00:00:00 2001 From: Nozomu Ikuta <16436160+NozomuIkuta@users.noreply.github.com> Date: Thu, 24 Aug 2023 02:52:17 +0900 Subject: [PATCH] docs: improve explanation about `body` option (#276) * docs: improve explanation about `body` option * add note about buffer too + improves * update --------- Co-authored-by: Pooya Parsa --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b9a1daad..df2a0423 100644 --- a/README.md +++ b/README.md @@ -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", {