question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make a post request with JSON data in application/x-www-form-urlencoded

See original GitHub issue
let data = {
    a: "some data",
    b: 33,
    c: [
        {
            name: "XYZ",
            other: true
        },
        {
            name: "ABC",
            other: true
        }
    ]
    d: {
        something: true
    }
}

How to make a POST request with above JSON data, as content-type application/x-www-form-urlencoded using fetch api?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:16
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

154reactions
teamehcommented, Sep 13, 2016

That makes sense.

Too bad we need yet another polyfill for that to work…

import URLSearchParams from 'url-search-params';

const searchParams = new URLSearchParams();
for (const prop in params) {
  searchParams.set(prop, params[prop]);
}


fetch(url, {
  method: 'POST',
  body: searchParams
})

And that only works for flat objects…

And than again, when the data is that simple, a manual conversion isn’t that hard as well.

const searchParams = Object.keys(params).map((key) => {
  return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
}).join('&');

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  },
  body: searchParams
})

Thanks for the quick reply and the insight!

29reactions
teamehcommented, Apr 13, 2016

@dgraham using

fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: new FormData(postData)
})

results in this formData why I try it…

------WebKitFormBoundarybJk1vnBHdSvQEKdD--

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I POST a x-www-form-urlencoded request using Fetch?
You can use FormData and URLSearchParams to post as application/x-www-form-urlencoded with the example below: If you have a form:
Read more >
Request body encoding: JSON? x-www-form-urlencoded?
My app was using application/json to make requests to my API but I found a downside to it. The preflighted OPTIONS request is...
Read more >
Content Type : x-www-form-urlencoded, form-data and json
Basically there are three ways to send the HTML data to the server. “Content Type : x-www-form-urlencoded, form-data and json” is published by...
Read more >
How to post JSON data using Curl? - ReqBin
The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.
Read more >
Add a Request Body to a POST Request | API Connector
A content-type header describes the object's format, so the server knows how to parse it. The default content type will be application/x-www-form-urlencoded.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found