2

I have this curl request that worked, and I hope to turn it into node.js code.

curl -H "Content-Type: application/json" -X POST -d '{"text":"ibm","date":{"from":"20170330","to":"20170530"},"restrictedDateRange":false}' https://finsights.mybluemix.net/api/query

However, I tried in my way but i am quite sure i did something wrong as the response body doesnt match what i got from the curl request.

My code that failed:

server.get('/hello', function create(req, res, next) {
    // //sample request

    request({
            url: "https://finsights.mybluemix.net/api/query",
            method: "POST",
            headers: {
                "content-type": "application/json",
                data: {
                    "text": "ibm",
                    "date": {
                        "from": "20170330",
                        "to": "20170530"
                    },
                    "restrictedDateRange": "false",
                }
            },
            json: true, // <--Very important!!!


        },
        function(err, res, body) {
            console.log(body);

        });
    // //end of sample request
    console.log("success");
    return next();
});

Please can anyone teach me how to transform it please?

dan
  • 1,944
  • 1
  • 15
  • 22
Carson Yau
  • 489
  • 3
  • 9
  • 17
  • for "restrictedDateRange" you sent the `string` "false", try a boolean to match the curl. – YvesLeBorg May 30 '17 at 15:48
  • 1
    I don't think your data should be a header. Try setting your request up like this - https://stackoverflow.com/a/16194906/4774345 – dan May 30 '17 at 15:49

1 Answers1

3

Your data shouldn't be a header. Try including your data in the request body, like this:

request({
    url: "https://finsights.mybluemix.net/api/query",
    method: "POST",
    headers: {
        "content-type": "application/json"
    },
    body: {
        "text": "ibm",
        "date": {
            "from": "20170330",
            "to": "20170530"
        },
        "restrictedDateRange": "false",
    },
    json: true
},
function(err, res, body) {
    console.log(body);
});
dan
  • 1,944
  • 1
  • 15
  • 22