如何 POST 使用 Express 中的请求
How to POST with request in Express
所以我正在尝试 POST 一个新用户使用 request 模块连接到 Intercom,但似乎无法获得正确的格式。我能够使用请求和 POST 使用相同的数据使用 CURL 来很好地执行 GET 请求。所以我的结论是我使用请求库的方式一定有问题。
工作 CURL 请求:
curl https://api.intercom.io/users \
-X POST \
-u xxx:da39xxxxxxxxxxxxxxxxx0709 \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' -d '
{
"user_id": "25",
"email": "wash@serenity.io",
"name": "Hoban Washburne",
"signed_up_at": 1392731331,
"last_seen_ip" : "1.2.3.4"
}
服务器端请求不工作:
request.post('https://api.intercom.io/users',
{
'auth': {
'user': 'xxx',
'pass': 'xxx',
'sendImmediately': false
}},
{
"user_id": "193",
"email": "test@test.io",
"name": "Hoban Washburne",
"signed_up_at": 1392731331,
"last_seen_ip" : "1.2.3.4",
},function (error, response, body) {
var info = JSON.parse(body);
console.log(info);
console.log(error);
});
res.status(200).send(info);
更改了请求的格式
这是一个对我有用的答案。
request({
'url': 'https://api.intercom.io/users', //URL to hit
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
'auth': {
'user': 'xxxxxx', //org ID
'pass': 'xxxxxxxxxxxxx', //API key
'sendImmediately': false
},
//Lets post the following key/values as form
'json': {
"user_id": "193",
"email": "test@test.io",
"name": "Hoban Washburne",
"signed_up_at": 1392731331,
"last_seen_ip" : "1.2.3.4"
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
res.status(200).send("this is a test");
});
所以我正在尝试 POST 一个新用户使用 request 模块连接到 Intercom,但似乎无法获得正确的格式。我能够使用请求和 POST 使用相同的数据使用 CURL 来很好地执行 GET 请求。所以我的结论是我使用请求库的方式一定有问题。
工作 CURL 请求:
curl https://api.intercom.io/users \
-X POST \
-u xxx:da39xxxxxxxxxxxxxxxxx0709 \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' -d '
{
"user_id": "25",
"email": "wash@serenity.io",
"name": "Hoban Washburne",
"signed_up_at": 1392731331,
"last_seen_ip" : "1.2.3.4"
}
服务器端请求不工作:
request.post('https://api.intercom.io/users',
{
'auth': {
'user': 'xxx',
'pass': 'xxx',
'sendImmediately': false
}},
{
"user_id": "193",
"email": "test@test.io",
"name": "Hoban Washburne",
"signed_up_at": 1392731331,
"last_seen_ip" : "1.2.3.4",
},function (error, response, body) {
var info = JSON.parse(body);
console.log(info);
console.log(error);
});
res.status(200).send(info);
这是一个对我有用的答案。
request({
'url': 'https://api.intercom.io/users', //URL to hit
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
'auth': {
'user': 'xxxxxx', //org ID
'pass': 'xxxxxxxxxxxxx', //API key
'sendImmediately': false
},
//Lets post the following key/values as form
'json': {
"user_id": "193",
"email": "test@test.io",
"name": "Hoban Washburne",
"signed_up_at": 1392731331,
"last_seen_ip" : "1.2.3.4"
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
res.status(200).send("this is a test");
});