[ Unsure if my Android app is sending valid POST requests ]
I have an app that at the moment I'm just sending some dummy data through a POST request to a Flask server. The flask server is not seeing the post data. When I send it to a sinatra server the post data is there no problem.
Android Code:
URL url = new URL(imageRequests[0].getUrl());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String urlParameters = "param1=a&param2=b&param3=c";
System.out.println(urlParameters);
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
Flask Code:
@app.route('/', methods=['POST'])
def submit():
print request.form
return "Uploaded"
Flask Output:
Loading from savePath test.tree
* Running on http://0.0.0.0:5000/
ImmutableMultiDict([])
10.100.85.69 - - [25/Jul/2014 17:09:07] "POST / HTTP/1.1" 200 -
Sinatra Code:
post '/' do
puts params
"Uploaded"
end
Sinatra Output:
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2014-07-25 17:07:27] INFO WEBrick::HTTPServer#start: pid=18674 port=4567
{"param1"=>"a", "param2"=>"b", "param3"=>"c"}
10.100.85.69 - - [25/Jul/2014 17:07:35] "POST / HTTP/1.1" 200 12 0.0053
10.100.85.69 - - [25/Jul/2014:17:07:34 BST] "POST / HTTP/1.1" 200 12
- -> /
I'm really confused as to why Sinatra is getting the post data, but flask isn't. My only guess is that the POST request from android is not quite right and that sinatra is more forgiving on that.
Is this the case?
Edit: Output from netcat listening over the port
POST / HTTP/1.1
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.4; Nexus 7 Build/KTU84P)
Host: 10.100.85.210:5000
Connection: Keep-Alive
Accept-Encoding: gzip
Content-Type: application/x-www-form-urlencoded
Transfer-Encoding: chunked
1b
?param1=a&param2=b&param3=c
0
Answer 1
Changed it to this. It seems to have been a problem with chunking the output
URL url = new URL(imageRequests[0].getUrl());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String urlParameters = "param1=value¶m2=value";
System.out.println(urlParameters);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes().length));
urlConnection.setRequestMethod("POST");
OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(urlParameters);
writer.close();