Troubleshooting “Invalid Character Looking For Beginning Of Value”

Austin Dewey
2 min readDec 11, 2020
Photo by AbsolutVision on Unsplash

Here’s an error I often come across with Golang applications:

invalid character '<' looking for beginning of value

This error is frustrating to those who first encounter it because it comes across as archaic. Where is the < character coming from, and why is it causing errors? What underlying library is throwing this, and how can you fix it?

Unfortunately, the resolution depends on a case-by-case basis. However, in this post, I hope to help troubleshoot to lead you down the right path.

The Root Cause

This error message is thrown by the encoding/json Golang package. This error happens when the json package attempts to unmarshal a string that is not JSON. Consider you are trying to unmarshal an HTML string using the JSON library:

<!DOCTYPE HTML>
<html>
<head>
. . .

The first character, <, is not a valid JSON starting character, hence the reason for the error “invalid character ‘<’ looking for beginning of value”.

Receiving an HTML response is not the only possible way to encounter this error. For example, if you were trying to unmarshal this string:

Hello world!

Then you would end up with the error “invalid character ‘H’ looking for beginning of value”.

Troubleshooting the Error

You will encounter this error when your application reaches out to an API and expects a JSON response but receives data in a different format instead. To troubleshoot, try to identify what endpoint your application is trying to reach when it encounters the error. Try to curl this endpoint manually from your command line. What kind of response do you get?

I ran into this issue earlier in the week and found that the app I was troubleshooting tried to call an API that didn’t exist!

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>404 - Page Not Found</title>
. . .

You may also receive a non-JSON response if you have failed to authenticate. Try to curl from the command line, passing in the -u flag for basic authentication (or -H “Authorization: Bearer $TOKEN” for OAuth) and see if you get a 401 (Unauthorized) or 403 (Forbidden). These errors will produce the error if the response is not in JSON format.

Of course, you can also use a debugger to resolve this error. In my case, I could curl from the command line to determine the issue, but a debugger can serve as a more robust alternative if you are still stuck.

Thanks For Reading

Hopefully, this post helps you move past this frustrating error. Ensure that the data your app is receiving is in JSON format, and curl endpoints manually to be sure your application is receiving JSON-formatted data. Feel free to use a debugger, too, if you’d like.

Originally published at https://austindewey.com on December 11, 2020.

--

--

Austin Dewey

Kubernetes enthusiast and not-terrible guitarist. Co-author of Learn Helm. | https://austindewey.com