HTTP error data

Started by DocteurWhoFR 2 posts 241 views

Reply on sbox.game
  1. edited Jul 2025 #1
    DocteurWhoFR 620
    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. #2
    dame dema 165
    ну заплачь