Skip to content Skip to sidebar Skip to footer

Sending Bearer Token With Httppost In Android

I couldn't find a way to authenticate my app with my server using the Bearer token I had created. it works perfectly with Postman though. I've tried using UTF-8 encoding, using ?ac

Solution 1:

The "Authorization" should not be a parameter. Its a header.

HttpPostrequest=newHttpPost(URL_SECURED_BY_BASIC_AUTHENTICATION);
Stringauth= DEFAULT_USER + ":" + DEFAULT_PASS;
byte[] encodedAuth = Base64.encodeBase64(
  auth.getBytes(StandardCharsets.ISO_8859_1));
StringauthHeader="Basic " + newString(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

HttpClientclient= HttpClientBuilder.create().build();
HttpResponseresponse= client.execute(request);

Solution 2:

I'm using Volley, but when I set up the headers, I do it with this:

HashMap<String, String> headers = newHashMap<String, String>();
    String authValue = "Bearer " + apiToken;
    headers.put("Authorization", authValue);
    headers.put("Accept", "application/json; charset=UTF-8");
    headers.put("Content-Type", "application/json; charset=UTF-8");

Solution 3:

Why don't you use OK Http for networking requests? Then you can do something like this:

val request = Request.Builder()
                .url(yourUrl)
                .header("Authorization", "Bearer $yourToken")
                .post(yourBody)
                .build()

Post a Comment for "Sending Bearer Token With Httppost In Android"