【问题标题】:RequestJS pass a content-type: application/jsonRequestJS 传递一个内容类型:application/json
【发布时间】:2026-02-12 16:15:01
【问题描述】:

我正在寻找一种使用 Nodejs 在 RequestJS 中传递内容类型的方法。

现在我有这个作为参数:

'use strict';
request = require('request'),

app.register = function(req, res) {
 request.post({
   headers: {'Content-Type' : 'application/json'},
   url: 'my.url.here',
   form: req.body,
 }).pipe(res);
}

但由于某种原因,服务器仍然说它是内容类型的 text/xml.. 谁能告诉我如何自定义这个?

【问题讨论】:

  • 另外,如果您将 content-type 设置为 application/json,为什么要在表单选项中发送 req.body? json != 表单参数。 requirejs 是如何与此联系在一起的?
  • Req.body 是传入控制器的参数...

标签: node.js requestjs


【解决方案1】:

您应该使用json 选项,而不是form,并且您不需要设置内容类型,json 选项会为您完成。

request.post({
  url: 'my.url.here',
  json: req.body
}).pipe(res);

【讨论】:

  • 谢谢,Kevin 解决了这个问题,刚接触这个 requestjs :)
最近更新 更多