Newtonsoft.Json.JsonReaderException: "Unexpected character encountered while parsing value: <. Path
Hei all, I am coding a REST API HTTP POST REQUEST, but I always get an error as soon as I start Building the Project. Might it be the way how my JSON is written ? or maybe something else ?
the code:
Thanks in advance for any help 🙂
14 Replies
It sounds like the response body is not JSON.
Hei @mtreit do you mean this line ?
Well you didn't say what line the exception happens on, so I am speculating, but yes since it's an error in the reader that seems like a possible candidate.
Oh yes sorry, I debugged it and the error occurs at that line
but the problem is, I dont know what to do
maybe its better to see the code this way
you're mostly getting some sort of unauthorized error from the API.
that code looks mostly fine(obviously there's some improvements but i won't go into them)
do a Console.WriteLine on the result before deserializing
make sure its valid json
Yes, you're probably getting HTML back instead of JSON. Print it out and see what it says.
ok thank you very much. but how do I deserialize it then if this way doesnt work ?
I will give you a feed back as soon as I an output from Console
if you're getting HTML back from the API, then that means you just have to make sure your request is valid. when your request is valid you will get valid json back
validity can be determined by = the info you send in your request, your api keys, the URL you're using for the request etc.
Hei hei, me again.
Thank you for the response. I have tryed the console output now and it gave me this
lol
it worked because a file has been created on server
but the deserialize doenst work
hehe
check out the docs here on how to make models -> https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to?pivots=dotnet-8-0
How to serialize and deserialize JSON using C# - .NET
Learn how to use the System.Text.Json namespace to serialize to and deserialize from JSON in .NET. Includes sample code.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.