We're creating small project in which we have flask as a FrontEnd and restlet based WebService.
We try to send login data as a JSON from flask to restlet:
def login():
error = None
if request.method == 'POST':
payload = {'username' : request.form['username'], 'password' : request.form['password']}
headers = {'Content-Type': 'application/json'}
req = requests.post(WEBSERVICE_IP + '/login', data=json.dumps(payload), headers=headers)
(...)
Flask based website is shouting about:
ValueError: No JSON object could be decoded
We have no idea how to reconcile communication between flask and restlet.
Edit (22-04 10:08pm GMT): I found out that responce was:
<html>
(...)
Unsupported Media Type
The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method
(...)
</html>
Edit (22-04 11:26pm GMT): I'm still not sure why, but I supposed that it may be something with JSON format. After correcting my code so it will send correct JSON (said JSONLint), I still get the same messages. Anyone know how to create JSONObject in Python? WebService have method:
@Post("json")
public JSONObject verifyAccount(JSONObject dane){
Edit (23-04 7:26pm GMT): Ok. So we're almost sure that it's problem with invisible header. Can anyone confirm that header creation in python code here is correct?
Edit (24-04 5:40pm GMT): Problem is still actual. As some other suggested, I changed requests back to the urllib2. This helped with the first thing - "Value problem". Now browser is having
urllib2.HTTPError
HTTPError: HTTP Error 415: Unsupported Media Type
POST Request now:
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
payload = {"Login": request.form['username'],
"Haslo": request.form['haslo']}
data = json.dumps(payload)
clen = len(data)
req = urllib2.Request(WEBSERVICE_IP + '/login', data,
{'Content-Type': 'application/json', 'Content-Length': clen})
f = urllib2.urlopen(req)
response = f.read()
f.close()
Edit (24-04 6:20pm GMT):
Wireshark captured POST request and it looks ok.
Ok. Solution was easier than I would think.
Problem was on the WebService side. It was resolved by changing JSONObject to JsonRepresentation:
@Post("json")
public JSONObject verifyAccount(JsonRepresentation data){