简体   繁体   中英

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

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?

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.

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

在服务器端和hapijs上使用bodyparsing,我碰巧碰到了做到这一点的hapi-bodyparser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM