Home > Enterprise >  what is the correct way to pass Bearer token in header section of my HTTP.Post in flutter
what is the correct way to pass Bearer token in header section of my HTTP.Post in flutter

Time:12-30

My Post API need a customer_id in body but also need a bearer token. I am passing it using following code

var myId="1005",
var token="my Token here"
    var response = await http.post(Uri.parse("http://haulers.tech/jashn/mobile/home/shoutouttype"),
      body: ({
         "customer_id":myId.toString,
         }),
      headers: ({
         "Authorisation": token.toString, //here I want to pass Bearer Token
         })
      );

This code return status code 401.

CodePudding user response:

Pay attention to the word Bearer its must be Capitalized other ways it wont work, not sure whats the reason, but for flutter http calls make sure to capitalize that word like this

var response = await httpClient.post(
url, 
headers:{ 
    "Accept": "application/json",
    "Content-type": "application/json",
    "Authorization": "Bearer $token"
},
body :{some body});

CodePudding user response:

Bearer tokens are usually sent preceded with Bearer : the following key value pair:-

"Authorization": "Bearer {TOKEN}"

Should work.

  • Related