Search code examples
javaandroidbottle

POST request is not sent


I have a short Android-Java client program which sends a basic information to bottle-python server with POST method. In the first version of code, server does not show anything. However, In the second version it works but I cannot understand what this additional line do because it has anything to do with posting content. I really appreciate if someone helps me figure this out.(There is nothing wrong with the server code since I can properly send request with python requests and my browsers).

This is the first version of client code:

String url = "http://192.168.1.23:8080/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
PrintStream myPusher = new PrintStream(os );
myPusher.print("param1=hey");

Second version:

String url = "http://192.168.1.23:8080/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
PrintStream myPusher = new PrintStream(os );
myPusher.print("param1=hey");
InputStream in= con.getInputStream(); //Nothing changed but only this additional line

Bottle(python) server:

@app.route('/', method="POST")
def hello():
    print("it works")
    name = request.forms.get("param1")
    print(name)
    return name

@app.route('/')
def hello():
    i=0
    print("it works")

run(app, host="192.168.1.23", port=8080)

With first client code server shows nothing.

With second code server shows:

it works
hey
192.168.1.24 - - [31/Dec/2018 17:10:28] "POST / HTTP/1.1" 200 3

Which is as I expected.


Solution

  • With your first code snippet the output stream is still open. So the server does not know if it got the complete request. Probably just closing the stream would work as well.

    However, I would make at least a call to getResponseCode to see the outcome of the request.