HTTP error data

Started by DocteurWhoFR · one year ago · 1 reply · 243 views

edited one year ago#1
DocteurWhoFR
Member
JoinedSep 2022 Posts14 Score620
ok, i dont known if i am stupid on that one, but how can i use Http.RequestJsonAsync and i have the error message when there is an http error 

like if there is an error, there is an exception, but this exception doesnt include the content of the error so i cant parse it like if that was 

{
"error": "this is the message of the error 4xx"
}

how i can retrieve it ?
edited:
that was a "stupid" one, by reading how RequestJsonAsync  was made, i have made my own that parse error

	async Task<(JsonModel, Error)> makeCallJsonWithError<JsonModel>( string url, string method, HttpContent content, Dictionary<string, string> headers, CancellationToken cancellationToken = default( CancellationToken ) )
	{
		headers["Authorization"] = "Bearer " + this.ApiToken;
		Log.Info( $"Requesting to {url}" );
		using HttpResponseMessage response = await Http.RequestAsync( this.BaseUrl + url, method, content, headers, cancellationToken );

		if ( response.IsSuccessStatusCode )
		{
			Log.Info( "call success" );
			return (await response.Content.ReadFromJsonAsync<JsonModel>( cancellationToken ), default(Error));
		}
		else
		{
			Log.Info( "call error" );
			Log.Info( await response.Content.ReadAsStringAsync() );

			return (default( JsonModel ), (await response.Content.ReadFromJsonAsync<ErrorRoot>( cancellationToken )).error);
		}
	}
#2
dame dema
Member
JoinedDec 2022 Posts4 Score165
ну заплачь
people
Log in to reply
You can't reply if you're not logged in. That would be crazy.