Reputation: 155
I have the following 2 routines:
public async Task<Token> GetToken(string username, string password)
{
var transurl = $"{burl}users/auth/";
using (var c = new HttpClient())
{
var cs = $"{{ \"username\": \"{username}\", \"password\": \"{password}\"}}";
var token = await PostDataAsync<Token>(c, transurl, cs);
return token;
}
}
public async Task<T> PostDataAsync<T>(HttpClient c, string url, string content)
{
var sc = new StringContent(content, Encoding.UTF8, "application/json");
var res = await c.PostAsync(url, sc); //this line seems to produce the error
if (res.IsSuccessStatusCode)
{
return await res.Content.ReadFromJsonAsync<T>();
}
else
{
var errs = await res.Content.ReadAsStringAsync();
var nordExc = JsonSerializer.Deserialize<Exception>(errs);
var nexc2 = new Exception(errs);
throw nexc2;
}
}
The error which I have got is
The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.
And as far as I can see it is the codeline await c.PostAsync(url, sc);
which reports this error. The value of the variable content
of the PostDataAsync routine in my case shows the string "{ "username": "myUsername", "password": "myPassword"}" which is a valid JSON format. I have checked the evaluation of this value in Visual Studio - and selected JSON Visualizer, and the content of the variable is fine. And because of the error the value of IsSuccessStatusCode is false.
How to handle this error?
Upvotes: -1
Views: 58