# I want to send token through HttpClient headers

**URL:** <https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199>\
**Category:** ionic-v3\
**Created:** [December 24, 2018, 10:43am UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199 "2018-12-24T10:43:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![flycoders\_sourav](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@flycoders\_sourav](https://forum.ionicframework.com/u/flycoders_sourav)\
**Post date:** [December 24, 2018, 10:43am UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199/1 "2018-12-24T10:43:33Z")

</div>

I want to send token through header how can i do that

```auto
   public details(token){
    const httpOptions = {
      headers: new HttpHeaders({
        'Accept': 'application/json',
        'Authorization':'Bearer'+token
      })
    }; 
    return this.http.post('http://mywebsite.ca/helpify/api/details', httpOptions)
   }

```

whenever i tired to hit this api its showing 401 Unauthorized  
how can i fix it please help me out  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![feaxer](https://avatars.discourse-cdn.com/v4/letter/f/34f0e0/32.png) [@feaxer](https://forum.ionicframework.com/u/feaxer)\
**Post date:** [December 24, 2018, 1:08pm UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199/2 "2018-12-24T13:08:22Z")

</div>

Like this.  
Authorization’:'Bearer '+token

You must check request. from chrome

must look like this [http://prntscr.com/lyzmzy](http://prntscr.com/lyzmzy)

---

<div class="post-metadata">

**Author:** ![flycoders\_sourav](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@flycoders\_sourav](https://forum.ionicframework.com/u/flycoders_sourav)\
**Post date:** [December 26, 2018, 5:53am UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199/3 "2018-12-26T05:53:53Z")

</div>

> [@feaxer](#):
>
> Authorization’:'Bearer '+token

Thanks for your response  
yes i tired but showing same problem 401 unauthorized

Actually am using laravel api

```auto
public function details(Request $request) 
    { 
        $user = Auth::user(); 
   
        $acceptHeader = $request->header('Accept','Authorization');
        if ($acceptHeader != 'application/json') {
            return response()->json([], 400);
        }
        return response()->json(['success' => $user], $this-> successStatus); 
        
    }

```

this is my console

 ![Screenshot_1](https://global.discourse-cdn.com/ionicframework/original/3X/1/a/1a34dc8c49f62697b65622e7c02a4f7df9d40b10.png)  
Thanks

---

<div class="post-metadata">

**Author:** ![feaxer](https://avatars.discourse-cdn.com/v4/letter/f/34f0e0/32.png) [@feaxer](https://forum.ionicframework.com/u/feaxer)\
**Post date:** [December 26, 2018, 7:40am UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199/4 "2018-12-26T07:40:04Z")

</div>

bro, you sent data. not header.

You must fixed İonic Code like this.

http.post(‘[http://mywebsite.ca/helpify/api/details](http://mywebsite.ca/helpify/api/details)’, data, httpOptions)

syntax

http.post('APİ URL,DATA,HTTPOPTİONS)

if you dont want data.

http.post('APİ URL,NULL,HTTPOPTİONS)

---

<div class="post-metadata">

**Author:** ![flycoders\_sourav](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@flycoders\_sourav](https://forum.ionicframework.com/u/flycoders_sourav)\
**Post date:** [December 26, 2018, 7:43am UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199/5 "2018-12-26T07:43:06Z")

</div>

Thanks for your response please let me try  
This is my full code

```auto
   public details(token){
    const headers = new Headers();
    headers.append('Content tyepe','application/x-www-form-urlencoded')
    headers.append('Accept', 'application/json' );
    headers.append('Authorization', 'Bearer ' + token); //<-- added space after 'Bearer'
    const options = new RequestOptions({ headers: headers });
      return this.http.post('http://mydomain.ca/helpify/api/details', options)                   
  }

```

@feaxer above code right or wrong?

---

<div class="post-metadata">

**Author:** ![flycoders\_sourav](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@flycoders\_sourav](https://forum.ionicframework.com/u/flycoders_sourav)\
**Post date:** [December 26, 2018, 7:48am UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199/6 "2018-12-26T07:48:54Z")

</div>

```auto
var data = null;

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "http://mywebsite.ca/helpify/api/details");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Authorization", "Bearer "+token);
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("Postman-Token", "f5009fbf-3bc2-4f58-ace0-b767f7a6092c");

xhr.send(data);

```

this above code is working fine but i i want to do in angular

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:03pm UTC](https://forum.ionicframework.com/t/i-want-to-send-token-through-httpclient-headers/151199/7 "2021-07-19T21:03:25Z")

</div>

Closed on July 19, 2021, 9:03pm UTC


