5

When sending a GET request to API, I'm always getting an error (so the response will fall into the CATCH instead of TRY) even tough status is 200.

Here is the request :

// Check if user exists at blur on email input authentication
checkUserExists( platformId: string, email: string) {
    return this.http.get(checkUserExistsAPI + `/${platformId}/${email}`);
}

As i'm using Angular 5, I took off the .map(res).

And here is the function that uses the request :

// Verify if emails exists
verifyEmail(email) {
    if (email) {
        this.authenticationService.checkUserExists( this.platformId, email )
            .subscribe(
                (data: CheckEmailResponse) => {
                    console.log(data);
                },
                (error: CheckEmailResponse) => {
                    console.log(error);

                }
            );
    }
}

It will never console.log(data) as I'm always getting this error :

error:{
    error: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at XMLHttpRequest.onLo…, text: ""}
    headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
    message:"Http failure during parsing for http://API.com"
name:"HttpErrorResponse"
ok:false
status:200
statusText:"OK"
4
  • Can you post what your response looks like? Maybe the response isn't valid JSON Commented Dec 1, 2017 at 12:20
  • The response is in my initial post, this is the very last code block. Commented Dec 1, 2017 at 12:24
  • That is the error message, I was referring to the JSON from the http get response, You can get it under network tab in chrome/firefox developer tools. Commented Dec 1, 2017 at 12:27
  • Actually, the response has no body content. Only the status is usefull to me. I guess Angular expects response to be json formatted but get no content Commented Dec 1, 2017 at 14:26

2 Answers 2

3

Ok so I figured it out. The response didn't had any content body and Angular 5 is, by default, expecting a Json formatted content body, therefor it couldn't work. I only needed the status response anyway to know if it was 200 or 404.

I added { responseType: 'text' } to the request as above`:

return this.http.get(checkUserExistsAPI +/${platformId}/${email}, { responseType: 'text' });

So now it managed to go to the TRY of my TRY/CATCH with a data equals to "null". But I don't care about the data as I only care about the status. Anyway, now if the status is 200, it will go into the TRY, if the status is 404, it will go into the CATCH.

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

Comments

0

This is what head requests are for. When you don't need the object, just metadata about the object, such as status code. See: https://angular.io/api/common/http/HttpClient#head

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.