how to convert x-www-form-urlencoded payload to json

575
July 05, 2017, at 03:04 AM

i am using the npm request module to post data. by default the posy is of x-www-form-urlencoded so on the server side i am receiving the data like this

{
  'files[0][path]': 'myfile1',
  'files[0][contents]': 'some content',
  'files[0][active]': 'true'
}

how do i parse the data to json? like

{
  files: [{
    path: 'mypath',
    content: 'mycontent',
    active: true
  } {
    path: 'mypath2',
    content: 'mycontent2',
    active: false
  }]
}

I think express js might use a middle wire for this, but I am using hapi server. what are my options to parse the payload to json?

Answer 1

with bodyparsing on the server side and hapijs, i happen to bump into hapi-bodyparser which did the job.

Answer 2

There are a number of existing node packages that deal with parsing requests. Check out https://www.npmjs.com/package/body-parser-json. To write a solution from scratch would just be a waste of time.

Answer 3

Have you looked into jQuery's serializeArray() api? This might be good enough depending on your use case but here's another library built on top of serializeArray().

Rent Charter Buses Company
READ ALSO
multiple promises running in parallel, $q.all needs chaining?

multiple promises running in parallel, $q.all needs chaining?

I have 3 parallel promises or api requests once all teh three are done, I need to call another api request based on the second promise and then finally callthen( of $q

272
line chart not working using react-d3-components in create-react-app

line chart not working using react-d3-components in create-react-app

I'm new to react and I'm trying to put react-d3-component in the create-react-app boilerplate

374
key questions to to ask myself to learn programming [on hold]

key questions to to ask myself to learn programming [on hold]

As I am learning how to program I find it a bit difficult to piece code togetherSo I joined stackoverflow for more help

291