Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: HttpErrorResponse :"Http failure during parsing for..." - a String returning successfully from server

Spring-boot RESTful server side; a testing method that will return a string:

@RequestMapping(value = "test", method = RequestMethod.GET)
    public ResponseEntity<String> test(HttpServletRequest req, HttpServletResponse resp) {
        try {
            return new ResponseEntity<String>("Test has worked, biatch!", HttpStatus.OK);
        } catch (Exception e) {
            System.err.println("## EXCEPTION: " + e.getMessage());
            return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

from Postman- everything works perfectly fine and I get the String returned parsed correctly from JSON.

However, when trying the same from my Angular client-side, I keep getting an HttpErrorResponse object generated.

  public url: string = "http://localhost:8080/theater/admin/test";
  constructor(private as: AdminService, private http: HttpClient) { }

  ngOnInit() {
  }

  getTest() {
    this.as.getTest()
      .subscribe(data => console.log(data), // this should happen on success
        error => console.log(error));  // this should happen on error
  }

funny enough, it contains the String returned from the server, and I can access it with error.text on the subscribe function. the Error object on console:

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8080/theater/admin/test", ok: false, …}
error
:
{error: SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttp…, text: "Test has worked, biatch!"}
headers
:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message
:
"Http failure during parsing for http://localhost:8080/theater/admin/test"
name
:
"HttpErrorResponse"
ok
:
false
status
:
200
statusText
:
"OK"
url
:
"http://localhost:8080/theater/admin/test"
__proto__
:
HttpResponseBase

This probably has to do with parsing the JSON object returning from the server, containing the String. however, returning objects, collections and whatever else- works totally fine- .subscribe() parses whatever objects I get from the server correctly, or if the server had an exception occurred, the returned HttpStatus correctly invokes an HttpErrorResponse on the client side.

So, what's up with Strings mis-firing like that? I'm always getting an HttpErrorResponse, no matter what. am I doing something wrong here?

like image 590
Maoration Avatar asked Sep 12 '18 20:09

Maoration


People also ask

What is Httperrorresponse?

HttpErrorResponselink A response that represents an error or failure, either from a non-successful HTTP status, an error while executing the request, or some other failure which occurred during the parsing of the response.


2 Answers

Test has worked, biatch!

This is not a JSON. Thus parsing error.

This probably has to do with parsing the JSON object returning from the server, containing the String. however, returning objects, collections and whatever else- works totally fine- .subscribe()

Well it worked for POJOs besause those are JSON encoded. Here you have plain String

To get response as string instead of object, do something like

 http.get(url, {responseType: 'text'})
like image 106
Antoniossss Avatar answered Oct 01 '22 04:10

Antoniossss


Try something like this -

{ responseType: 'text' as 'json' }

I was facing the same issue with HttpClient in Angular 7, resolved using the above setup.

The problem is when you use a return type on the get or post or any method from httpClient like this-

this.http.get<FooClass>(url)

It assumes that the response type is JSON and thus it expects a JSON object in response. Setting the response type as 'text' as 'json' will force text response.

like image 32
Rajesh Panda Avatar answered Oct 01 '22 04:10

Rajesh Panda