[英]How can i get the Json of my url request?
I want to make an URL request, its outcome would be a JSON
after I have tried it in the browser. 我想发出一个URL请求,在浏览器中尝试后,其结果将是
JSON
。 I want to put the entire JSON response in a var or const for further processing afterward. 我想将整个JSON响应放入var或const中,以便以后进行进一步处理。
What i have tried so far: 到目前为止我尝试过的是:
app.use(express.bodyParser());
app.post(MY_URL, function(request, response){
console.log(request.body);
console.log(request.body;
});
Then : 然后 :
app.use(bodyParser.urlencoded({
extended: true
}));
app.post(MY_URL, function (req, res) {
console.log(req.body)
});
Both of which haven't worked out and being a beginner in node.js
isn't helping. 两者都还没有解决,并且成为
node.js
的初学者没有帮助。
Edit: In order to clarify my question: 编辑:为了澄清我的问题:
my_url = https://only_an_example
That URL typed in the browser will give a Json in that page like the following: 在浏览器中键入的URL将在该页面中提供一个Json,如下所示:
{
"query": "testing",
"topScoringIntent": {
"intent": "Calendar.Add",
"score": 0.987683
},
"intents": [
{
"intent": "Calendar.Add",
"score": 0.987683
},
{
"intent": "None",
"score": 0.0250480156
}}
What i want is to get that Json response and print it using node.js. 我想要得到的是Json响应并使用node.js打印它。
If you are trying to get the body of the request, just access to: 如果您尝试获取请求的正文,请访问:
req.body;
If you want to send a JSON object as response, you can do: 如果要发送JSON对象作为响应,则可以执行以下操作:
var objectToResponde = {"key1": "value1", "key2": "value"};
res.send(objectToResponde);
After further understanding the OP's problem (via the comments below) you can convert the object you want to send back to the client to JSON like so: 在进一步了解了OP的问题(通过以下注释)之后,您可以将要发送回客户端的对象转换为JSON,如下所示:
app.post('some/url', (req, res) => {
const myObject = {a: 1, b:2};
res.json(myObject);
});
The outcome of this is a JSON response with the appropriate response headers set. 结果是设置了适当的响应头的JSON响应。
Try this: 尝试这个:
app.use(bodyParser.urlencoded({
extended: true
}));
app.post(MY_URL, function (req, res) {
res.status(200).json(<your object>);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.