简体   繁体   中英

Unable to get the correct data format in req.body

I'm trying to parse the body to nodeJS but I get it back in the wrong format.

This is my code:

fetch("http://localhost:5000/email", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },

      body: JSON.stringify({ shareUrl: props.shareUrl })
    });`

This is how it is parsed when console.log(body.req.shareUrl) in Node

{ '{"shareUrl":"link"}': '' }

this is how I want it back

{ 'shareUrl': 'link' }

EDIT

When I try to send it from Postman it works fine

在此输入图像描述

I get following output:

{ Name: 'jej' }

The Content type:

{ "Content-Type": "application/x-www-form-urlencoded" }

is not ment to send the JSON data in the first place.

If you want to send and receive JSON, change the content type to:

{ "Content-Type": "application/json" }

like:

fetch("http://localhost:5000/email", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: { data: json.stringify({shareUrl: props.shareUrl}) }
    });`

On server you will find it under req.body.data

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