简体   繁体   中英

How do i receive post data at server same as client post data format using nodejs?

I have a post data in the format below

{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}

Expecting the same format on the server but received the format below

{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[["-102.038635","36.800488"],["-94.567716","36.800488"],["-96.149793","43.381098"],["-104.060178","43.508721"]]}}}

You can see the difference in coordinates property. Double quotes are added to each value in server received coordinates array. How can I get the coordinates array same as client post data?

My application configured with expressJS and using body-parser. Any additional settings required to fix the problem?

app.use(bodyParser.urlencoded({ limit: '50mb' }));
app.use(bodyParser.json({ limit: '50mb' }));

Thanks in advance.

First of all, from @bumblebeen , HTTP understands everything as a string and as such converts everything to a string. It's up to you to parse it using Number.parseInt

Also, it is irrelevant but express now ships with it's own body parser which you can use. So, rather than calling bodyParser.json , you simply call express.json

Edit

Per the comment stated, you could have a function called parseCoordinates

function parseCoordinates(coordinates) {
     return coordinates.map(inner => {
         return Array.isArray(inner) ? inner.map(elem => parseFloat(elem)) : parseFloat(inner);
     });
}
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}

The above is actually a JavaScript Object.

{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[["-102.038635","36.800488"],["-94.567716","36.800488"],["-96.149793","43.381098"],["-104.060178","43.508721"]]}}}

where as the above is actually a JSON.

As one of the answer suggested, whenever you send data to server, everything in the payload will automatically be converted to strings. You should cast the values as numbers.

Also, I don't think any parse would be able to do it and you should write your own code for that. The reason I say it is because, the parser would never know if the original value was a string sent by the client or was actually a number since all the keys and values will become strings.

Assum I have

var payload = {"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}

Using JSON.stringify(payload) to stringify this before send server.

payload = JSON.stringify(payload)
// after this code then sending payload to server

In the server using JSON.parse(payload) to use this

// server nodejs
var receivedContent = JSON.parse(payload)
// then you can using receivedContent with format you want

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