gpt4 book ai didi

node.js - 将 request.post 的内容类型 header 设置为 json

转载 作者:搜寻专家 更新时间:2023-10-31 23:32:27 24 4
gpt4 key购买 nike

我正在为我的项目使用“hackathon-starter” Node 组。在此构建中,当我尝试从 request.post 调用 API 时,它将采用所有 API 的“Content type 'application/x-www-form-urlencoded;charset=utf-8' header 。我已尝试通过 API 调用更改 header ,但只需要

Content type : 'application/x-www-form-urlencoded;charset=utf-8'

所有 API 的 header 。我试过下面的代码。我想为所有 API 设置 application/json

var querystring = require('querystring');
var request = require('request');

var form = {
"userType": req.body.type,
"userName": req.body.mobile,
"email": req.body.email,
"name": req.body.name,
"password": req.body.password
};

var formData = querystring.stringify(form);
var contentLength = formData.length;
request.post({
headers: {'content-type':'application/json'},
url:'mylink',
form: formData // I have tried form as well.
},function(error, response, body){
console.log(body)
});

我在控制台上的错误消息。

{"timestamp":1484822264270,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=utf-8' not supported","path":"mylink"}

最佳答案

我猜你需要根据你的要求使用 json 选项:

  var form = {
"userType": req.body.type,
"userName": req.body.mobile,
"email": req.body.email,
"name": req.body.name,
"password": req.body.password
};

request.post({
url:'mylink',
json: form,
},function(error, response, body){
console.log(body)
});

来自选项文档:

json - sets body to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.

关于node.js - 将 request.post 的内容类型 header 设置为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com