1
votes

I was trying out angular2 for JavaScript and I was able to make a Http.get request but when I tried to make a Http.post request it would return 415 Unsupported Media Type error.

The code itself is pretty concise, so I was not sure what could be causing this error. How would I resolve this issue?

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

console.log(headers.get('Content-Type'))            

return this.http.post(this.endpoint_url, data="test.json", headers=headers).subscribe(
    function(response){console.log("Success Response" + response)},
    function(error){console.log("Error happened : " + error)});
2
415 will almost definitely be something returned by your server.Jack Guy

2 Answers

0
votes

Your code is simply trying to post the string test.json as body of the request, not the content of test.json file.

Also notice that you are using the = symbol at the this.http.post() arguments. That is assigning the values to the vars and returning them.

In practice your code is the same as:

return this.http.post(this.endpoint_url, "test.json", headers).subscribe(...

And "test.json" is not a valid JSON string (which seems to be what you want, due to the headers you are setting).

If you want to send test.json's contents, you should first HTTP GET it, an only then send the result as body of the post.

0
votes

try stringifying your json before entry

 this._http.post(this.standardUrl,
            JSON.stringify({
                ClientVersion: '1.0.0.0',
                ClientLanguage: 'en'
            }),  { headers: headers } )
        .subscribe((response: Response) => {
                data = response.json();
            });

That will at least provide a json object..