简体   繁体   中英

POST request with a json body javascript

I need to make a POST request with a json object in the body. The request is the following:

request({
        encoding: 'utf8',
        url: 'myUrl',
        json: true,
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-Auth-Token': ticket
        },
        json : JSON.stringify(eval("(" + str + ")"))
        }, function (error, response, body) {
            console.log(JSON.stringify(eval("(" + str + ")")))
            res.status(200).send('added')
            }
        )

The request doesn't do what it's supposed to do. However, if I use the JSON.stringify evaluation that i get with console.log to make a POST request to the same url with Postman, it works. Any ideas why?

Why not just

json: JSON.stringify(str)

Forget the eval() function, it's not needed. str should be a javascript value, which will be converted to a JSON string.

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