[英]How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch
This is a CURL example which works fine: 这是一个可以正常工作的CURL示例:
curl --request POST \
--url <url> \
--header 'authorization: Bearer <authorization token>' \
--header 'content-type: application/x-www-form-urlencoded' \
--data 'category=1&userId=<uuid>'
I'm trying to reproduce this request using isomorphic-fetch . 我正在尝试使用isomorphic-fetch重现此请求。
I've tried this: 我已经试过了:
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
})`
But I get a 411
status code error response ( length required ) 但是我收到
411
状态码错误响应( 需要长度 )
More info about URLSearchParams
and Fetch
here: 有关
URLSearchParams
和Fetch
更多信息,请URLSearchParams
:
Any suggestions? 有什么建议么?
Assuming the server implementation is correct the problem here is with isomorphic-fetch (or much more likely, the underlying GitHub's WHATWG Fetch polyfill) in that it doesn't add the Content-Length header as it's required to for fixed-length bodies by the Fetch Standard. 假设服务器实现是正确的,这里的问题是同构提取(或者更可能是底层的GitHub的WHATWG Fetch polyfill),因为它没有添加Content-Length标头,因为它对于定长主体是必需的。提取标准。
(You should also be able to omit the Content-Type header as that is also supposed to be inferred from the URLSearchParams object and added by the implementation of the API.) (您还应该能够省略Content-Type标头,因为应该从URLSearchParams对象中推断出该标头,并通过API的实现来添加它。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.