How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch

rfc1484

This is a CURL example which works fine:

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.

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)

More info about URLSearchParams and Fetch here:

fetch documentation

fetch spec

Any suggestions?

Anne

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.

(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.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send post request with x-www-form-urlencoded body

How to get and parse JSON response from x-www-form-urlencoded POST, RestTemplate (Java)?

How to force Angular2 to POST using x-www-form-urlencoded

Retrofit + POST method + www-form-urlencoded

Send sensitive data using POST+application/x-www-form-urlencoded

how to post data in node.js with content type ='application/x-www-form-urlencoded'

How do I post data using okhttp library with content type x-www-form-urlencoded?

Post 'x-www-form-urlencoded' content with aurelia-fetch-client

How to POST with multipart/form-data header and FormData using fetch

Swift - How to send POST request with "x-www-form-urlencoded" content-type

How to post (x-www-form-urlencoded) Json Data using Retrofit?

How to post a x-www-form-urlencoded data properly using javascript?

How to POST content as application/x-www-form-urlencoded

ASP.NET Core Fetch POST FormData() application/x-www-form-urlencoded

How to POST x-www-form-urlencoded in retrofit

How to send post request with content-type x-www-form-urlencoded android retrofit

POST using cURL and x-www-form-urlencoded in PHP returning Access Denied

How to escape + in post call content type as application/x-www-form-urlencoded

fetch() header shows as application/x-www-form-urlencoded instead of application/json

How to send post request with x-www-form-urlencoded body with loopj-async-http library

How to convert x-www-form-urlencoded post Message to JSON post Message?

How to send x-www-form-urlencoded in a post request in webclient?

POST application/x-www-form-urlencoded Body to REST API using Jitterbit

how to post body x-www-form-urlencoded using webclient?

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

Server-side Blazor Post using HttpClient with x-www-form-urlencoded

Trying to make an API POST using application/x-www-form-urlencoded in a wpf desktop app

How to post an array of values using `UrlFetchApp.fetch` with `application/x-www-form-urlencoded`?

How to do a x-www-form-urlencoded POST login using cypress?