C
C#7mo ago
RBMKBlaze

Reading request data as JSON error

A few days ago i started working on a program that uses JSON requests to communicate with an API, it worked for a day after it i started getting a wheird error (from Newtonsoft.JSON), the status code is 500 (Internal Server Error) and it happend when the request returned valid JSON data, now i get an error (in the attachment) the status code is still 500. How can i fix it, does it have to do anything with me or the API?
No description
10 Replies
RBMKBlaze
RBMKBlaze7mo ago
Error happends on await response.Content.ReadFromJsonAsync<LoginReturned>();
No description
RBMKBlaze
RBMKBlaze7mo ago
No description
Sossenbinder
Sossenbinder7mo ago
Can you just get the response as a raw string for the moment? 500 is usually a server side error, so I wouldn't rely on receiving a valid response json at all If you grab the raw string you can validate what's coming back
RBMKBlaze
RBMKBlaze7mo ago
yep sure give me a minute 500 happend and it returned valid data
Sossenbinder
Sossenbinder7mo ago
Yeah, that is still valid, though it might also be a json with error details you can then not parse with your "successful" model as <T>
RBMKBlaze
RBMKBlaze7mo ago
how could i exactly do that, im a bit confused response.Content.ReadAsStringAsync() returns just "Internal Server Error", how can i get the json data without having to prase a class
RBMKBlaze
RBMKBlaze7mo ago
how my returning data should look like, the json properties are name exactly as the api docs said
No description
Sossenbinder
Sossenbinder7mo ago
Well that's your issue then, the response body is not even valid json. I was actually trying to point out that your server might return { "LoginName": "name" } in a successul case, but { "Error": "...", "Status": 500 } in an error code, so you can't cover both kinds of response with a single ReadFromJsonAsync<LoginReturned>(). In your case it seems like the server doesn't even respond with a JSON but rather just returns a raw error string which is even less parseable. But before trying to parse the content, you can check the response.StatusCode or response.IsSuccessStatusCode to react accordingly
RBMKBlaze
RBMKBlaze7mo ago
alrighty, altho wheird cus the server was sending a valid json while the statuscode being 500
Sossenbinder
Sossenbinder7mo ago
Yeah, that's actually also common practice, there's a standard called ProblemDetails to avoid just that issue - Servers returning arbitrary errors But of course, I can't tell what the server actually does Just trying to explain to you what might be the issue for your json parsing to fail