user10011538
user10011538

Reputation: 137

MeteorJS HTTP POST Request Connection Lost XMLHttpRequest

I am writing an application that integrates into a website that is already written in Meteor (I can't change that but I can add on to it). I am trying to send information from the Meteor application to my Flask server.

To do this I am using MeteorJs's HTTP module.

The code for this:

HTTP.post('http://127.0.0.1:5000/path', {
    "content" : {"headers" : {"Content-Type": "application/json"}, "data": {time: getTime, data: getData()}}
}, 
(error, result) => {
    if(error){
        console.log(error);
        console.log({time: getTime(), data: getData()})
        }
    else {
        console.log(result);
        }
    }
)

getTime() and getData() both work independently outside this function, so they shouldn't be the source of error.

When I look at the JS console for when the event is being fired I receive the following message: Error: Connection lost at XMLHttpRequest.xhr.onreadystateexchange and what was supposed to be sent to the Flask server.

When I look at the Flask server I see that it is receiving the post request with status code 200, but it seems like there is no data actually being received.

The code on the python end:

@app.route(r'path', methods=["POST"])
def get_data():
    print(request.data)
    print(request.args)
    return "Hello World"

The print statements come out empty with this being shown on the console b'[object Object]' or ImmutableMultiDict([])

The Meteor app and the Flask app are both on different ports.

The problem I believe is on the MeteorJS side, since I used the curl linux function it works properly when I ping the flask server from there.

Is there a way to fix this error? If so how?

Upvotes: 1

Views: 214

Answers (1)

Sean Hayes
Sean Hayes

Reputation: 351

Hi "parameters" should be "data". You can find all valid options in the docs. Let me know if it works for you.

HTTP.post('http://127.0.0.1:5000/path', {
        data : {time: getTime(), data: getData()}
    }, (error, result) => {
        if(error){
            console.log(error);
            console.log({time: getTime(), data: getData()})
        } else {
            console.log(result);
        }
    }
)

Upvotes: 0

Related Questions