I am working on express js and using the request package for http methods
我正在使用快速js并使用http方法的请求包
request({
url:'http://custom-url',
method:'GET'},function(err,response,body){
console.log("Got Response : "+respnose.statusCode);
console.log("Body : "+body);
console.log("name is "+body.name);
})
My output is :
Got Response : 200
Body :{"name":"John","id":"139321"}
name is undefined
The body has a name parameter but i don't understand why body.name is undefined, please help !
正文有一个名称参数,但我不明白为什么body.name是未定义的,请帮忙!
1 个解决方案
#1
0
Sometimes the response comes in
String
.有时响应来自String。
Try to parse it in JSON
object then use it
尝试在JSON对象中解析它然后使用它
request({
url: 'http://custom-url',
method: 'GET'
}, function(err, response, body) {
if (body && typeof body == "string") {
body = JSON.parse(body);
}
console.log("Got Response : " + respnose.statusCode);
console.log("Body : " + body);
console.log("name is " + body.name);
})
#1
0
Sometimes the response comes in
String
.有时响应来自String。
Try to parse it in JSON
object then use it
尝试在JSON对象中解析它然后使用它
request({
url: 'http://custom-url',
method: 'GET'
}, function(err, response, body) {
if (body && typeof body == "string") {
body = JSON.parse(body);
}
console.log("Got Response : " + respnose.statusCode);
console.log("Body : " + body);
console.log("name is " + body.name);
})