I am trying to implement a simple JSON
post to the URL that accepts JSON
in body with basic authentication on ANDROID
.
I have tried with HttpUrlConnection
but I get "unauthorized"
and my data gets send to the server.
Another way I tried is using HttpClient
, but now I get a different problem. Authentication works perfect but the data is not sent to the server...
Just to be sure, I setup a small test in a simple Java project (not Android Environment).
This is the code that I am using to POST
to the server:
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler<String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://localhost/api/v1/purchase/");
postMethod.setEntity(new StringEntity("{\"amount_adult\" : 1, \"object_id\" : 13}"));
postMethod.setHeader( "Content-Type", "application/json");
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
postMethod.setHeader("Authorization", authorizationString);
String response = httpClient.execute(postMethod,resonseHandler);
System.out.println("response :" + response);
The code in the Java project works perfect.
When I try exact same code in Android, I get internal server error
from the server, which means that JSON data has not been received.
I really don't understand why this is working in JAVA but not in Android.
The effect that I would like to achieve, is the following command:
curl --dump-header - -H "Content-Type: application/json" -X
POST --data '{"amount_adult":1, "object_id":13}'
--user travelbuddy:travelbuddy http://localhost/api/v1/purchase/
To send basic authentication credentials to the server, you need to convert the "username: password" pair to a Base64 encoded string and pass it in the authorization request header. Where: Authorization: standard HTTP authorization header.
Firstly, we create an HttpClient, which can be used to execute HTTP requests. Secondly, we create an HttpRequest using the builder design pattern. The GET method sets the HTTP method of the request. The uri method sets the URL where we would like to send the request.
String authorizationString = "Basic " + Base64.encodeToString( ("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT);
...
The solution is a bit simpler actually:
String authorizationString = "Basic " + Base64.encodeToString(
("travelbuddy" + ":" + "travelbuddy").getBytes(),
Base64.NO_WRAP); // <=== Do not add '\n'
Finally, everything is sorted. Anyone that might experience this problem following is the solution.
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
postMethod.setHeader("Authorization", authorizationString);
This Base64.encodeToString appends an extra "\n" to the end of the authorization string. Then on the server next line after "\n" character is treated as body of the request and of course incorrect.
By replacing all new lines in authorization string will solve the issue:
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
authorizationString.replace("\n", "");
postMethod.setHeader("Authorization", authorizationString);
Easy fix but hard to find ;-)
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