简体   繁体   中英

How to turn this curl request to a node.js request please?

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?

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM