简体   繁体   中英

Python Flask outgoing post request is converted to GET using Passenger WSGI

I had this issue yesterday and investigating with help I've come across with this problem.

Flask is not getting any POST data from a request

So to sum up (you can see more details on my other post):

This piece of code is sending a POST request from my APP to my APP but not internally but as if it was other APP.

dat = dict( api_key="a-key-goes-here" )
request = requests.post(url, data=dat)
message = request.text
code = request.status_code

But my handling function for this request has empty request.form, request.args and request.data .

This is because the outgoing POST request from that code is being treated as if it was GET and the data is never handled. Also if I try to request.args the ImmutableMultiDict is empty.

The most weird thing is that this works if the APP is run trough Flask Server. It only fails if its handled from Apache Passenger WSGI.

Any clues of how to fix this?

Thanks in advance. If more info is needed I'm willing to update, just call it.

Update 1

As requested I copied my function from the other question to here for easier visualization.

@app.route('/loan_performer', methods=["POST"])
def loan_performer():
    if 'api_key' in request.form and request.form['api_key'] == API_KEY:
        ret = dict()

        # rate1 return a random number between 3.000 and 4.000 and point1 will be 0
        ret['rate_one'] = random.randint(3000, 4000)
        ret['point_one'] = 0

        # rate2 do it between 3.500 and 4.500, point2 being 0.5
        ret['rate_two'] = random.randint(3500, 4500)
        ret['point_two'] = 0.5  

        # rate3 between 4.000 and 5.000 with 1.0
        ret['rate_three'] = random.randint(4000, 5000)
        ret['point_three'] = 1.0

        return json.dumps(ret), 200

    else:
        return u"Your API Key is invalid.", 403

This is the response when I remove "GET" method and the code returned is 405

(08/03/2014 06:58:05 AM) INFO This is message <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

This is the response when I put "GET" method and the code is 404

(08/03/2014 07:01:54 AM) INFO This is message <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

I AM very sure that the URL is the correct one because otherwise I wouldnt receive a 405 error.

After a lot of work and headaches and no results we finnally switched to use GET instead of POST.

The problem seems to be in Apache configuration. Since we are using a shared server in Dreamhost it seems that they've got some kind of anti DDoS/spam service where all outgoing requests are converted to GET to avoid flooding.

Ill use what you ask when we move to a new non-shared server and we can configure Apache at our likings. IF the problem persists then Ill open a new question. Thanks for all the support and help!

I've looked at the other issue you're referring to. Have you considered handling the HTTP request type explicitly ? For example:

from Flask import request

@app.route('/my_resource', methods=['GET', 'POST'])
def handle_resource():
    if request.method == 'GET':
        # Do GET stuff
    if request.method == 'POST':
        # Do POST stuff

This way you can handle both POST and GET requests.

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