Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - HttpClient JSON POST with Basic Authentication

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/
like image 365
user1674948 Avatar asked Sep 15 '12 22:09

user1674948


People also ask

How do I post JSON string with basic authentication?

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.

How do I add basic authentication to HttpClient Java?

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.


2 Answers

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'
like image 167
Dmitry T. Avatar answered Nov 16 '22 02:11

Dmitry T.


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 ;-)

like image 32
user1674948 Avatar answered Nov 16 '22 02:11

user1674948