0

I am trying to do put request to the server ,but I am getting 401 error

The provider

putData(){
header = header.append('Content-Type', 'application/json');
        header = header.append('Accept', 'application/json');
        header = header.append('Authorization', 'Bearer');

      return this.http.put('http://something//', {headers: header})
          .subscribe((result: any) => {
               console.log(result);
          }, (errorResponse) => {
              console.error(errorResponse);

}

home.ts

this.MyProvider.putData();

I have attach the network header as well since the first request success, but the second does not ?!!!

enter image description here

enter image description here

Nouf
  • 733
  • 1
  • 11
  • 32
  • Are you authorized to do a put? See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401 – Phonolog Oct 07 '18 at 11:41
  • I found what the issue was the header was not being set , now the question how to set it – Nouf Oct 07 '18 at 12:15
  • Possible duplicate of [How to correctly set Http Request Header in Angular 2](https://stackoverflow.com/questions/41133705/how-to-correctly-set-http-request-header-in-angular-2) – Phonolog Oct 07 '18 at 12:22

1 Answers1

0
import { HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';

  constructor(public http: HttpClient )
putData(){

 const headers = new HttpHeaders({'Authorization':'Basic'});
     return this.http.post("'http://something//'", {headers: headers});
}

i think you code header is not adding properly try this method to add header

  • I tried it ,but it did not work this what worked for me return this.http.put('url' Option , { headers:({'Authorization': 'Bearer '+ token }) }) .map((result) => { console.log(result); }, (errorResponse) => { console.log(errorResponse); – Nouf Oct 07 '18 at 20:29