Angular 2 – Receiving a POST Response Header with Content-Length > 0 but no content

With Angular 2 I’m using Http.post() to retrieve some JSON data. When I use cURL or HttpRequestor everything works correctly. However, with Angular I’m getting a Response Header with Content-Length > 0 but the Response itself has no data.

public retrieveJsonData() {
    var url = 'https://somewhere.com/whatever/GetItem?token=XXXX&resource=YYYY&data=%7B%22id%22%3A2207%7D';
    let options = new RequestOptions({});

    return this.http
        .post(url, '', options)
        .map(result => result.json())
        .catch(this.handleError);
}

Request Headers:

POST /whatever/GetItem?token=XXXX&resource=YYYY&data=%7B%22id%22%3A2207%7D HTTP/1.1
Host: somewhere.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:45.0) Gecko/20100101 Firefox/45.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: http://localhost:8888/
Content-Length: 0
Content-Type: text/plain;charset=UTF-8
Origin: http://localhost:8888
Connection: keep-alive

Response Headers:

HTTP/1.1 200 OK
Date: Tue, 12 Apr 2016 22:49:23 GMT
Server: Apache/2.2.15 (CentOS) mod_ssl/2.2.15 OpenSSL/1.0.1e-fips
Set-Cookie: Foo=6djdhg7pnsqavk0nqa83u7rqt6; path=/; secure
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 1311
Keep-Alive: timeout=15, max=1000
Connection: Keep-Alive
Content-Type: text/json

So in this example the Content-Length = 1311 but the response is empty. If I use a tool like HttpRequester the POST works as expected and the response is 1311 characters. What is going on?

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Here is how I do post requests

        var headers = new Headers();
        headers.append('Content-Type', 'application/json');

        this.http.post(url, JSON.stringify({firstName:'Joe',lastName:'Smith'}),{headers:headers})
            .map((res: Response) => res.json())
            .subscribe((res:Person) => this.postResponse = res);

Try this after replacing with your values.