Search code examples
node.jsxmlhttprequestpython-requests

Post request with Node fails but works with Python


I'm trying to use a sentiment analysis API for some tweets I've streamed. The API in question is this: http://sentiment.vivekn.com/docs/api/. I've done this before in Python before and it worked as expected. I made a post request using the requests library and sent a JSON object with my content. The JSON object looked something like this:

{
    "txt": "The content of the tweet."
}

In Python, sending the post request looked something like this:

url = "http://sentiment.vivekn.com/api/text/"
data_dict = {
    "txt": "hi"
}

r = requests.post(url,json.loads(json.dumps(data_dict)))

print(r.text)

Now I'll admit I'm new to Javascript and web based programming in general, but I assume the logic should be similar in both languages. I tried using the XMLHttpRequest method but it always returned an internal server error with status code: 500.

The website works, it takes post requests and responds with the analysis, but I can't get it to work with Node. This is what I'm working with in Javascript:

const rp = require('request-promise');

var options = {
    method: 'POST',
    uri: 'http://sentiment.vivekn.com/api/text/',
    body: {
        "txt": "This is a very negative sentence, so we should get a negative analysis!"
    },
    json: true // Automatically stringifies the body to JSON
};

rp(options)
    .then(function (parsedBody) {
        console.log("Request received");
        console.log(parsedBody);
    })
    .catch(function (err) {
        console.log("Something went wrong\n" + err);
    });

It always catches an error with status code 500. I've tried several other methods including making the request with XMLHttpRequest. Nothing seems to work. It would be great if someone could point out where I'm going wrong.


Solution

  • This isn't an answer, but I thought it useful to show some code that evokes a different response, which may be a clue that will help debug the problem.

    I get the same response with curl:

    jim-macbookpro:~/development/node/so$ curl -X POST 'http://sentiment.vivekn.com/api/text/' -H "Content-Type: application/json" -d '{"txt": "hi"}'
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <title>500 Internal Server Error</title>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in theapplication.</p>
    

    I changed the example to use 'node-fetch', and I don't get 500, rather I get 405 - METHOD NOT ALLOWED.

    My suspicion is that this is a problem with the server being somehow very particular about the format of the request.

    I hope this helps.

    const fetch = require('node-fetch');
    
    fetch('http://sentiment.vivekn.com/api/text', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            txt:
                'This is a very negative sentence, so we should get a negative analysis!'
        })
    })
        .then(function(parsedBody) {
            console.log('Request received');
            console.log(parsedBody);
        })
        .catch(function(err) {
            console.log('Something went wrong\n' + err);
        });