// HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
static readonly HttpClient client = new HttpClient();
static async Task APITest()
{
// Call asynchronous network methods in a try/catch block to handle exceptions.
try
{
HttpResponseMessage response = await client.GetAsync(****************);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Debug.Log(responseBody);
}
catch(Exception e)
{
Debug.Log("\nException Caught!");
Debug.Log("Message :{0} ",e.Message);
}
it's printing only the error code (like 404 or 400) but not the error message.
But the error message is printed when checked in postman along with the error code.
Any insights, will help.
CodePudding user response:
I found this blog with very good example: http://nodogmablog.bryanhogan.net/2016/07/getting-web-api-exception-details-from-a-httpresponsemessage/
also try getting the message from response 's properties, not from the exception.
CodePudding user response:
In general when working with Unity you don't want to have a Main
method! Unity already provides the application framework and lifecycle!
Then also in general in Unity you don't want to use Console.Write
but rather Debug.Log
!
In your specific case Debug.LogException
so simply do
Debug.LogException(e);
which will log the entire exception including stack trace and everything to the console / the log file.
And finally you would rather use UnityWebRequest.Get
instead.