Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http post - how to send Authorization header?

How do you add headers to your http request in Angular2 RC6? I got following code:

login(login: String, password: String): Observable<boolean> {     console.log(login);     console.log(password);     this.cookieService.removeAll();     let headers = new Headers();     headers.append("Authorization","Basic YW5ndWxhci13YXJlaG91c2Utc2VydmljZXM6MTIzNDU2");     this.http.post(AUTHENTICATION_ENDPOINT + "?grant_type=password&scope=trust&username=" + login + "&password=" + password, null, {headers: headers}).subscribe(response => {       console.log(response);     });     //some return } 

The problem is, that angular doesn't add Authorization header. Instead of that, in request I can see following additional headers:

Access-Control-Request-Headers:authorization Access-Control-Request-Method:POST 

and sdch added in Accept-Encoding:

Accept-Encoding:gzip, deflate, sdch 

Unfornately there is no Authorization header. How should I add it correctly?

Whole request sent by my code looks as follow:

OPTIONS /oauth/token?grant_type=password&scope=trust&username=asdf&password=asdf HTTP/1.1 Host: localhost:8080 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Access-Control-Request-Method: POST Origin: http://localhost:3002 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Access-Control-Request-Headers: authorization Accept: */* Referer: http://localhost:3002/login Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8,pl;q=0.6 
like image 571
Maciej Treder Avatar asked Sep 09 '16 09:09

Maciej Treder


People also ask

How do I send a post Authorization header?

To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.

How can I send Authorization token in post request?

To send a POST JSON request with a Bearer Token authorization header, you need to make an HTTP POST request, provide your Bearer Token with an Authorization: Bearer {token} HTTP header and give the JSON data in the body of the POST message.

How do I create an Authorization header?

The command requires the valid user name and password (or API token) in the application to which you want to connect, and it encodes the credentials with base64. In the Authorization Header field, you enter the word "Basic" (which is the Authorization header type), a space, and then the base64-encoded credentials.


1 Answers

Ok. I found problem.

It was not on the Angular side. To be honest, there were no problem at all.

Reason why I was unable to perform my request succesfuly was that my server app was not properly handling OPTIONS request.

Why OPTIONS, not POST? My server app is on different host, then frontend. Because of CORS my browser was converting POST to OPTION: http://restlet.com/blog/2015/12/15/understanding-and-using-cors/

With help of this answer: Standalone Spring OAuth2 JWT Authorization Server + CORS

I implemented proper filter on my server-side app.

Thanks to @Supamiu - the person which fingered me that I am not sending POST at all.

like image 186
Maciej Treder Avatar answered Sep 21 '22 18:09

Maciej Treder