[英]fetch() header shows as application/x-www-form-urlencoded instead of application/json
[英]How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch
這是一個可以正常工作的CURL示例:
curl --request POST \
--url <url> \
--header 'authorization: Bearer <authorization token>' \
--header 'content-type: application/x-www-form-urlencoded' \
--data 'category=1&userId=<uuid>'
我正在嘗試使用isomorphic-fetch重現此請求。
我已經試過了:
const searchParams = new URLSearchParams();
searchParams.set('category', category);
searchParams.set('userId', userId);
return fetch(`<url>`, {
method: 'POST',
headers: {
'Authorization: Bearer <authorization token>',
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
body: searchParams
})`
但是我收到411
狀態碼錯誤響應( 需要長度 )
有關URLSearchParams
和Fetch
更多信息,請URLSearchParams
:
有什么建議么?
假設服務器實現是正確的,這里的問題是同構提取(或者更可能是底層的GitHub的WHATWG Fetch polyfill),因為它沒有添加Content-Length標頭,因為它對於定長主體是必需的。提取標准。
(您還應該能夠省略Content-Type標頭,因為應該從URLSearchParams對象中推斷出該標頭,並通過API的實現來添加它。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.