I have a python Flask listener waiting on port 8080. I expect another process to make a series of POST's to this port.The code for listener is as follows.
#!/usr/bin/env python2
from __future__ import print_function
from flask import Flask, request
from werkzeug import secure_filename
from datetime import datetime
import os, traceback, sys
import zlib
import ssl
import json
import os
import base64
app = Flask('__name__')
@app.route('/',methods=['GET','POST','OPTIONS'])
def recive_fe_events():
try:
data = request.get_data()
if request.content_length < 20000 and request.content_length != 0:
filename = 'out/{0}.json'.format(str(datetime.now()))
with open(filename, 'w') as f:
f.write(data)
print('Wrote', filename)
else:
print("Request too long", request.content_length)
content = '{{"status": 413, "content_length": {0}, "content": "{1}"}}'.format(request.content_length, data)
return content, 413
except:
traceback.print_exc()
return None, status.HTTP_500_INTERNAL_SERVER_ERROR
return '{"status": 200}\n'
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=False,port=8080)
However whenever I try to trigger an event to be pushed to the above listener.It seems that I am getting OPTIONS
instead of POST
.
192.168.129.75 - - [20/May/2015 14:33:45] "OPTIONS / HTTP/1.1" 200 -
192.168.129.75 - - [20/May/2015 14:33:45] "OPTIONS / HTTP/1.1" 200 -
192.168.129.75 - - [20/May/2015 14:33:51] "OPTIONS / HTTP/1.1" 200 -
192.168.129.75 - - [20/May/2015 14:33:51] "OPTIONS / HTTP/1.1" 200 -
The investigation of my client revealed that it expects the following flags in its response
to OPTIONS
.
Access-Control-Allow-Origin value_1
Access-Control-Allow-Methods value_2
Access-Control-Allow-Headers value_3
How do I format the above response
to OPTIONS
so that my server can start receiving POST
messages from the client.
post() method is used to generate a POST request. This method has can contain parameters of URL, params, headers and basic authentication. URL is the location for sending the request. Params are the list of parameters for the request.
Flask automatically process OPTIONS requests. To get access for cross domain request you API must have Access-Control-Allow-Origin header. It can contain specific domains, but if you want allow requests from any domains you can set it to Access-Control-Allow-Origin: * .
You need to set up your application for CORS. The easiest was is to use Flask-CORS.
This is old, but i'd like to actually answer the question...
In preflight a browser, and probably toolkit, sends an OPTIONS-request to figure out if it's safe to send a POST or PUT. Those are the typical cases...
Note that the headers should be on an OPTIONS-request only, they are redundant in the actual POST/PUT... cases.
I return the following in response-headers for an OPTIONS:
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '300'
You might want to read the spect for more details.
I find the Flask-CORS somewhat redundant, but thats a matter of taste perhaps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With