'Why Python cant send POST request to Flask
is this possible?
from flask import Flask, Response, render_template, redirect, url_for, request, flash, jsonify
import requests, json
app = Flask(__name__)
@app.route('/')
def home():
return '<p>Home</p>'
@app.route('/url2', methods = ['POST'])
def url2():
return {'code': '200'}
def request_to_flask():
if 1 + 1 == 2:
requests.post('http://127.0.0.1:5000/url2', {'somekey': 'somevalue'})
request_to_flask()
I got this error
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /url2 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000025CF2705030>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
And when I test from Insomnia I get no errors and {"code": "200"}
This is running in virtual env
py -m venv env
Procfile
web: gunicorn app:app
pip3 freeze > requirements.txt
certifi==2021.10.8
charset-normalizer==2.0.12
click==8.1.3
colorama==0.4.4
Flask==2.1.2
gunicorn==20.1.0
idna==3.3
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
requests==2.27.1
urllib3==1.26.9
Werkzeug==2.1.2
I even first-run only flask part
from flask import Flask, Response, render_template, redirect, url_for, request, flash, jsonify
app = Flask(__name__)
@app.route('/')
def home():
return '<p>Home</p>'
@app.route('/url2', methods = ['POST'])
def url2():
return {'code': '200'}
. and then from another py file send post req and got 405
import requests, json
def request_to_flask():
if 1 + 1 == 2:
requests.post('http://127.0.0.1:5000/url2', {'somekey': 'somevalue'})
request_to_flask()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
