1

I am trying to make a third party authorisation. When I hit a POST request to server, it sends HTML as response.

Header

private httpOptions = {
    headers: new HttpHeaders({
        'Accept': 'text/html',
        'Content-Type': 'application/json'
}),
    responseType: 'text',
};

API Call

return this.http.post(this.authUrl, this.httpOptions)
    .subscribe(data => {
        console.log(data);
    });

I am getting a SyntaxError:

Unexpected token < in JSON at position 0 at JSON.parse

1
  • 1
    you are missing http body argument Commented Sep 22, 2018 at 18:54

2 Answers 2

5
 private httpOptions = {
    headers: new HttpHeaders({
        'Accept': 'text/html',
        'Content-Type': 'application/json'
    }),
    responseType: 'text'
};

Your HTTP options had an extra comma after responseType:'Text'

return this.http.post(this.authUrl,null, this.httpOptions)
        .subscribe(data => {
            console.log(data);
        });

Also, HTTP POST for HttpClientModule takes in payload as second argument within the POST call.

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

1 Comment

"Your http options had an extra comma after responseType:'Text'". It is still valid code.
1

In my opinion the best way to put your response type is when you are calling the http post this will stopped the HttpClient from trying to parse the empty response as a JSON object.

 this.http.put(
 url,
 data,
 {
    headers: new HttpHeaders().set('Content-Type', 'application/json'),
    responseType: 'text' 
 }
 ).subscribe(  ....   );

with post

return this.http.post(url, params, {headers : headers, withCredentials : true,responseType: 'text'});

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.