如何在 node.js 中的 http PUT 正文中使用 JSON?

How do I use JSON in the body of an http PUT in node.js?

请参阅下面的代码。我正在尝试在我的请求正文中发送 { "status" : "accepted" } 。我不断从 API 返回的错误是:

{"message":"Unable to parse JSON in request body.","code":"invalid_json"}

我可以在 Swift 中完成这项工作,但我正在使用带有设置的字典对象并将其序列化。我不知道如何在 Node.JS.

中做到这一点
var https = require('https')

var options = {
    "host": "sandbox-api.uber.com",
    "path": "/v1/sandbox/requests/" + req.body.request_id,
    "method": "PUT",
    "headers": { 
    "Authorization" : "Bearer " + req.body.bearer_token,
    "Content-Type" : "application/json",
    },
    "body" : {
        "status" : "accepted"
    }
}

callback = function(response) {
    var str = ''
    response.on('data', function(chunk){
        str += chunk
    })

    response.on('end', function(){
        console.log(str)
    })
}

https.request(options, callback).end()

你将它写入请求对象,如:

var https = require('https')

var options = {
  "host": "sandbox-api.uber.com",
  "path": "/v1/sandbox/requests/" + req.body.request_id,
  "method": "PUT",
  "headers": { 
    "Authorization" : "Bearer " + req.body.bearer_token,
    "Content-Type" : "application/json",
  }
}

callback = function(response) {
  var str = ''
  response.on('data', function(chunk){
    str += chunk
  })

  response.on('end', function(){
    console.log(str)
  })
}

var body = JSON.stringify({
  status: 'accepted'
});
https.request(options, callback).end(body);