简体   繁体   English

如何使用同构提取使用application / x-www-form-urlencoded标头和URLSearchParams进行POST

[英]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: 有关URLSearchParamsFetch更多信息,请URLSearchParams

fetch documentation 获取文档

fetch spec 提取规格

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.

相关问题 fetch()标头显示为application / x-www-form-urlencoded而不是application / json - fetch() header shows as application/x-www-form-urlencoded instead of application/json 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded 如何在节点js中使用参数为application / x-www-form-urlencoded类型的POST调用 - How to consume POST call where parameter is of type application/x-www-form-urlencoded in node js 如何在 node.js 中发布内容类型 =&#39;application/x-www-form-urlencoded&#39; 的数据 - how to post data in node.js with content type ='application/x-www-form-urlencoded' 如何使用“内容类型”:“ application / x-www-form-urlencoded”发出发布请求? - How to make a Post request with “content type”: “application/x-www-form-urlencoded”? Axios post form urlencoded 请求 application/x-www-form-urlencoded - Axios post form urlencoded requests application/x-www-form-urlencoded 如何使用 Axios 在 application/x-www-form-urlencoded 中编码 JSON 数据? - How to encode JSON data in application/x-www-form-urlencoded using Axios? 使用 apollo-datasource-rest 库将 Content-Type 标头设置为 application/x-www-form-urlencoded - Setting Content-Type header to application/x-www-form-urlencoded using apollo-datasource-rest library node-fetch 发送 post 请求,正文为 x-www-form-urlencoded - node-fetch send post request with body as x-www-form-urlencoded 具有 application/x-www-form-urlencoded 格式的 Node.js Axios POST 请求? - Node.js Axios POST request with application/x-www-form-urlencoded format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM