Convert encoded application/x-www-form-urlencoded post data to json object

lance-p

A client wants to be able to make xmlhttp ajax requests with the default content type of content-type:"application/x-www-form-urlencoded; charset=UTF-8" but sending the data in the form the API expects application/json. So the request comes across as this:

  var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://localhost:80/api/metadata/taxonomy",
  "method": "POST",
  "headers": {
    "cache-control": "no-cache",
    "postman-token": "62a245ad-a0a2-4dd3-bf84-37f622f00b7d"
  },
  "processData": false,
  "data": "{\n\t\"practice\": [\"Learning\"]\n}"
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

But the API expects to be able to get req.body as a JSON object it can immediately use:

"{"practice":["Learning"]}"

Can I transform this "{\n\t\"practice\": [\"Learning\"]\n}" to this "{"practice":["Learning"]}" in some safe/suggested manner? (without some home grown parsing function or regex)

pfg

Yes, the JSON.parse function can be used for this:

try{JSON.stringify(JSON.parse(data))}

will convert strange json with newlines to standard one line string json.

JSON.parse Parses the json into an object

JSON.stringify Turns an object into a one line formatted JSON object

try JSON.parse will fail if an invalid json string is passed. (\ns are valid in json.parse)

If this you meant converting "{\n\"your\":\n \"object\"}" to a javascript object like {"your": "object"}, you can just use try{JSON.parse(variablename)}

According to these answers, for older browsers you may need JSON-js

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert json to x-www-form-urlencoded

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

CORS issue when trying to do JQuery post with json data, but not with plain text or x-www-form-urlencoded

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

How to convert below JSON data to application/x-www-form-urlencoded?

How to convert x-www-form-urlencoded string to json in dart?

my request failed when the post 'content-type' is application/x-www-form-urlencoded and * form field param= { <this is a json object>}

PYTHON: requests.post() how to send request_body encoded as application/x-www-form-urlencoded

Retrofit + POST method + www-form-urlencoded

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

Ansible URI module post form-urlencoded JSON data

How test Post request with custom object in content type application/x-www-form-urlencoded?

Trouble parsing URL encoded URI using read(data, "application/x-www-form-urlencoded") function in Dataweave 2.0

Postman form-data and x-www-form-urlencoded work but raw json doesn't

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

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

Post controller recognizes only parameters sent by "from-data" or "x-www-form-urlencoded" from postman

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

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

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

Use RestTemplate with object as data and application/x-www-form-urlencoded content type?

Sending "application/x-www-form-urlencoded" data in place of JSON over network

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

application/x-www-form-urlencoded or multipart/form-data?

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

Jersey client Post Request with x-www-form-urlencoded Fails

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

Spring - wrong encoding POST request with x-www-form-urlencoded

Post a x-www-form-urlencoded request from React Native