3

I am trying to do a simple Check Login method using Angular 2(2.0.0-rc.1) in a service. When I attempt to set the content-type to application/json it never sets the headers when sending to my web api backend.

import { Injectable } from '@angular/core'; import { HTTP_PROVIDERS, Http, Response, Headers, RequestOptionsArgs, RequestMethod, Request, RequestOptions } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import { Other Modules Left out for Security } from 'Hidden'; @Injectable() export class LoginService { private _http: Http; constructor(http: Http) { this._http = http; } CheckLogin(model: CredentialModel) { let url = 'http://localhost:51671/api/Login'; let data = JSON.stringify(model); let headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('Accept', 'application/json'); let requestOptions = new RequestOptions({ method: RequestMethod.Post, url: url, headers: headers, body: data }); console.log(requestOptions); console.log(data); return this._http.post(url, data, requestOptions); } } 

Request from Web Api

OPTIONS /api/Login HTTP/1.1 Host: localhost:51671 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Access-Control-Request-Method: POST Origin: http://localhost:5616 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2754.0 Safari/537.36 Access-Control-Request-Headers: content-type Accept: */* DNT: 1 Referer: http://localhost:5616/ Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 

Using the exact same data using PostMan works fine! Looks like there is an issue with Angular 2 sending application/json using the POST method.

Any help resolving this issue would be helpful. Open to trying any suggestions.

7
  • Can you try it without requestOptions please? i.e return this._http.post(url, data, {headers: headers}); Commented Jun 2, 2016 at 15:25
  • 1
    This is how I do it: this._http.post(url, body, { headers }) (haha, echonax :D) Commented Jun 2, 2016 at 15:25
  • Might be github.com/angular/http/issues/71 Commented Jun 2, 2016 at 15:26
  • 1
    You have to allow CORS requests, check this answer, or search for preflight request, angular2 cors Commented Jun 2, 2016 at 15:36
  • I've tried each step here and still getting the same error. For those of you who have it working are you using the latest Angular2 RC1? Commented Jun 2, 2016 at 21:02

3 Answers 3

7

The post method of the Http class accepts an object of type RequestOptionsArgs and not RequestOptions.

class RequestOptionsArgs { url : string method : string | RequestMethod search : string | URLSearchParams headers : Headers body : any withCredentials : boolean } 

You can specify it literally this way within the third parameter:

return this._http.post(url, data, { headers: headers }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Was that changed in never angular 2 versions, because RequestOptions works now in my code!
Just remember to import { Headers }
1

Setting the Headers in Angular 4 and greater should be set like this now:

let headers = new HttpHeaders({ "content-type": "application/json", "Accept": "application/json" });

this.http.post("apiUrl", data, {headers: this.headers})

Comments

0

RequestOptionsArgs is an interface in Angular2, so you can specify RequestOptions as a value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.