简体   繁体   中英

Why isn't the request in nodejs sending anything to an api that is a python app?

I've spent about a week on this, with no luck whatsoever. I've used chatGPT even to create the program specifically, but it still doesn't work with no inkling as to why.

I am trying to send a post request from nodejs to a python app. The issue: nothing happens on either end. The request happens, but it just gets lost in some void even though the url is exatcly correct.

here is the code:

nodejs:

const request = require('request');

const options = {
  method: 'POST',
  url: 'thecorrectpath/api/text',
  headers: {
    'Content-Type': 'text/plain'
  },
  body: 'Hello World'
};

request(options, (error, response, body) => {
  if (error) {
    console.error(error);
    return;
  }

  console.log(body);
});

Python:

from flask import Flask, request

app = Flask(__name__)

@app.route('/api/text', methods=['POST'])
def receive_text():
    text = request.data.decode('utf-8')
    print(text)
    return 'Text received'

if __name__ == '__main__':
    app.run(debug=True)

Both apps are running(via repl.it), nothing changes when the req is sent. Here is the python app total console:

hello
 * Serving Flask app 'main'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on IP
Press CTRL+C to quit
 * Restarting with stat
hello
 * Debugger is active!
 * Debugger PIN: pinhere

Here is the nodejs app total console:

sent
Error: socket hang up
    at connResetException (internal/errors.js:607:14)
    at TLSSocket.socketOnEnd (_http_client.js:493:23)
    at TLSSocket.emit (events.js:327:22)
    at TLSSocket.EventEmitter.emit (domain.js:467:12)
    at endReadableNT (internal/streams/readable.js:1327:12) {
  code: 'ECONNRESET'
}

I get that Err after about 2 minutes since the request was sent. Since nothing is ever logged in python app when this is sent, im assuming it never gets to it, even though the URL is perfectly correct.

Please help/suggestions to debug.

Turns out the issue was with repl.it. I created a python template app instead of a flask app.

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