C
C#2y ago
Ice_

❔ Bad request (400) - web api

I am not sure why this is happening because it works splendid with one class and not with another class for whatever reason. The code is basically identical in the controllers (and repositories). I don't have the code to show you because it's code from a friend of mine earlier today through Zoom. What we discovered was is that the POST method does not execute in the controller at all. The 400 error message is thrown before it actually connects to the API. It works through Swagger though, so there's an issue with the httpclient I guess but I can't really seem to pinpoint what the issue is. We are using PostAsJsonAsync in this case. My question for this whole thread is: Can I somehow print out the details of the "bad request" error code? What part is "bad"? The error codes gives you a good pointer but when you really can't see what the hell is going wrong, you want the details. So how do I get them? 😅
4 Replies
Henkypenky
Henkypenky2y ago
HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler()));

//ADD CODE BELOW - Use client



//ADD CODE ABOVE - Use client

public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("Request:");
Console.WriteLine(request.ToString());
if (request.Content != null)
{
Console.WriteLine(await request.Content.ReadAsStringAsync());
}
Console.WriteLine();

HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();

return response;
}
}
HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler()));

//ADD CODE BELOW - Use client



//ADD CODE ABOVE - Use client

public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("Request:");
Console.WriteLine(request.ToString());
if (request.Content != null)
{
Console.WriteLine(await request.Content.ReadAsStringAsync());
}
Console.WriteLine();

HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();

return response;
}
}
Ice_
Ice_OP2y ago
Thank you @Henkypenky! I'll give it a try with my mate as soon as I can!
Henkypenky
Henkypenky2y ago
np
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server