Lakie Lakie - 5 months ago 129
Android Question

how to send/POST JWT token from Android to Laravel backend

i wanted to access my back-end using android phone. at this moment i want to do two things:


  1. login the mob app after being authenticated from the Back-end.

  2. Upload data to the back-end (back-end is PHP with Laravel Framework).



i first send email and password to the backend and gotten back a response of
JWT
token as shown:

"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cLzE5Mi4xNjguMS4xMDI6ODAwMFwvYXBpXC92M1wvYXV0aGVudGljYXRlIiwiaWF0IjoxNDY2MDA5OTY3LCJleHAiOjE0NjYwMTM1NjcsImp0aSI6IjZjYjBlMTRjYTNkMjAzM2Q4OWM0NzM1M2ZjNjMzZTU2In0.GJGUjgy8U-uqjLSqJcysDTmgrNvxBHH03iBflLjsOwA"

Once the above token returned to the mob app, i want to send the same returned token for further post request, because the above jwt token is my key for accessing the back-end.

So my problem lies on sending the same token back to the back-end. it seems simple and stright forward since i have already started to communicate with my back end, and i have also checked my response by using post man.

loging in

and i can also got the user cridential using the jwt token on postman.

postman receive user cridentials as json

now the same token which works on postman is not working for my android. i know that my httpClient and httpPost are working since i have already send the email and password with it. i also know that my android post request is reaching the server since my returned result comes with an error message i built for an accepted token request, as shown below.

when android recives user cridentials as json

as you can see from the above snap shot. i first get the token inside a quotation (first highlighted), when posted for authentication. so i removed the quotation and posted the same token for getting user cridential but this time i got an error response which i built it on the Back-end.

so that is why i think my token is not going to the server properly. but i couldn't fix the problem. but i am guessing the token size is big, with a length of 490. so how shall i attach my token with the httpPost request? my code for the building the request is shown below:

public String getJsonString2(String url, String jwtString) {

String jsonString = "";

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List nameValuePair = new ArrayList(1);
nameValuePair.add(new BasicNameValuePair("token", jwtString));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}
catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}

// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
jsonString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();

} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}

return jsonString;

}


i have also tried using MultipartEntityBuilder for parsing the parameter (token in my case) but the MultipartEnityBuilder library was crasshing my program when building:

the MultipartEnityBuilder library is added to my project using the following dependencies:

//for accessing the MultipartEntity lbrary
compile "org.apache.httpcomponents:httpcore:4.2.4"
compile "org.apache.httpcomponents:httpmime:4.3"


the error because of the MultipartEntity

build error for the MultipartEntity library

So now my question is:
how can i send the jwt token value from android to Laravel backend.

Answer

i have managed to solve my problem by simply setting the Authorization header with the token:

public String getJsonString2(String url, String jwtString) {

    String jsonString = "";

    // Creating HTTP client and post
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);

    httpPost.setHeader("Authorization", "Bearer \\{" +  jwtString + "\\}");

    // Making HTTP Request
    try {
        HttpResponse response = httpClient.execute(httpPost);
        jsonString = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        System.out.println("Http String content: " + jsonString);
    } catch (ClientProtocolException e) {
        // writing exception to log
        e.printStackTrace();
    } catch (IOException e) {
        // writing exception to log
        e.printStackTrace();
    }
    return jsonString;
}