0

I want to make an URL request, its outcome would be a JSON after I have tried it in the browser. I want to put the entire JSON response in a var or const for further processing afterward.

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.

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:

{
  "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.

red
  • 27
  • 5
  • 1
    It seems you need to access "response.body" or "res.body" . Not request – Hasan Dec 14 '17 at 11:24
  • Can you add it a full response in answers if you figured out what's wrong or have a better way to suggest. – red Dec 14 '17 at 11:26
  • If you want to get Json then use `app.get` , `app.post` is just for sending data to the server but i can also return json data – Neji Soltani Dec 14 '17 at 11:27
  • I want to send the URL then retrieve the output so guess i'll need to use both? – red Dec 14 '17 at 11:29
  • Possible duplicate of [Calling a JSON API with Node.js](https://stackoverflow.com/questions/11826384/calling-a-json-api-with-node-js) – Liam Dec 15 '17 at 13:09

3 Answers3

0

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:

var objectToResponde = {"key1": "value1", "key2": "value"};
res.send(objectToResponde);
David Vicente
  • 3,091
  • 1
  • 17
  • 27
0

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:

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.

Luke Stoward
  • 1,480
  • 14
  • 24
  • app.use(bodyParser.json()); router.post(myUrl, (req, res) => { const data = req.body; console.log(data) session.send(data) }); That's what i've done and i didn't get the output Json – red Dec 14 '17 at 11:37
  • And you're definitely posting the data? Are you able to debug the client and confirm the data is being sent as JSON and the correct `content-type` header is being set? – Luke Stoward Dec 14 '17 at 11:39
  • I don't want to send Json, i want to send a URL then retreve the Json response then print out the Json response to make sure it's what i want. – red Dec 14 '17 at 11:41
  • That literally makes no sense. You have a client 'A' that is posting data to the server 'B'. 'A' is posting to an endpoint exposed by 'B'. What is the 'A' posting to 'B'? Also in your code what is the value of `MY_URL` – Luke Stoward Dec 14 '17 at 11:44
  • I'm still struggling to make sense of this. Your NodeJS instance is running your express server right, it can accept requests coming from a client and return a response to client. It can look at the JSON body sent from the client and it can return a JSON response. So you want to just return the example JSON in your post to the client when the client requests the a particular URL? – Luke Stoward Dec 14 '17 at 11:57
  • The client only sends The Url tho he doesn't send a Json, that URL typed in a browser will give a Json only in that page, nothing else, what i want is that Json, i want to put it in a var or const – red Dec 14 '17 at 11:59
  • Ok, I think I understand, I have updated my solution. – Luke Stoward Dec 14 '17 at 12:06
  • getting an error, router is not defined, plus i didn't understand this part const myObject = {a: 1, b:2}; – red Dec 14 '17 at 12:16
  • `myObject` in the example is the object you want return in JSON format to the browser which would be displayed on screen. Replace `router` with `app`. – Luke Stoward Dec 14 '17 at 12:34
0

Try this:

app.use(bodyParser.urlencoded({
   extended: true
}));

app.post(MY_URL, function (req, res) {
    res.status(200).json(<your object>);
});
HOTAM SINGH
  • 121
  • 2
  • 11