![](/img/trans.png)
[英]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.