How-to Fix “Http failure during parsing” Error

By | August 6, 2019

Http failure during parsingIn my frontend application, I was making requests to the backend API, but in some cases, I was getting the following error: Http failure during parsing for http://localhost/api/get-data. If you are facing the same problem welcome on board!

Reasons for”Http failure during parsing for” error

Invalid JSON format

One of the possible reasons is that you have an incorrect format of your JSON. Please go to DevTools -> Network tab and copy the response. Validate it via JSON validator

Wrong ResponseType

Depending on the Content-Type header, your browser can try to parse the response to a JSON object. You can try setting the responseType option before making the request to avoid that:

return this.http.post(`${this.endpoint}/account/login`,payload, { ...options, responseType: 'text' })

Invisible \0 character (null character) in the response

If you already validated JSON from DevTools in the JSON validation tool and setting the responseType didn’t help, try this one. Get the json from your backend (in case of C# you can put a breakpoint in VS and copy result JSON before sending the response back) and see if there are any \0 characters. In my case, one of the fields contains an exception stack trace from the outside service. Sometimes there was \0 character in the middle of the stack trace. DevTools in Chrome automatically ignore this character but HttpClient is having a hard time parting JSON with null character in the middle.

To fix this in C# you can do something like this:

result.FieldWithStackTrace = result.FieldWithStackTrace?.Replace("\0", string.Empty);

Leave a Reply

Your email address will not be published. Required fields are marked *