6

I've just upgraded to Angular 2 RC 5 and for a reason I can't figure out, I cannot make any http request (ie. the request is never made / doesn't show up in browser logs).

This is code that worked until today's upgrade.

console.log("Let's call github");
let url = 'https://raw.githubusercontent.com/angular/angular/master/LICENSE';
return this.get(url).do(d=>console.log('success'));

When my component calls the function containing this code, Let's call github is output, but then the request is not made. I'm sure that Observable and Http from @angular/http are imported properly since this same code worked this morning. My first thought was that I was missing the HttpModule, but I've confirmed that I import it into my main AppModule

In fact, if I comment out the HttpModule import, it makes no difference I can see. The error is the same, as follows:

Exception: TypeError: this._body is null

Here is an excerpt of the stack trace (at the bottom is the line calling the function quoted above): enter image description here

1 Answer 1

8

Update 2016-08-12

This has been addressed and will probably be fixed in the next release. A current workaround is to skip the content-type setting on GET requests, or to explicitly set body='' as a request option.

Link: Gihub discussion

Original answer

The error seems to be with a block of code in a function I run before each http request. Because the API I interact with expects JSON content, I have the following (options refers to the RequestOptions object used with every request):

//set content to JSON
if(!options.headers.has("Content-Type")){
    options.headers.append("Content-Type", "application/json")
}

If I comment out these lines, the request is made successfully. So I'm thinking that starting with Angular 2 rc 5, this block throws an error if the request body is empty (as is the case with my get request from the OP).

If my intuition is correct, this change in behavior is caused by this commit (new to rc 5)

fix(http): convert objects passed to requests into a string

I bet that when I set the content-type header, Angular tries to be helpful and stringify the request body for me. Of course there is none with a GET, hence the error.

Sign up to request clarification or add additional context in comments.

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.