C# – JSON value could not be converted to System.String

When you send a request to ASP.NET with a JSON body, you get the following exception: System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1. You can’t deserialize JSON to a string. You’re either using a string parameter with [FromBody] or your model has a string … Read more

How to set the Content-Type header in Postman

When you want to use Postman to send a request with content in the Body, you have to first select a content type option (none, form-data, etc…). When you select the type and add content, Postman automatically generates the Content-Type header. You can see the auto-generated Content-Type header in the Headers tab. The following table … Read more

ASP.NET Core – Four basic ways to receive parameters

There are four basic ways to receive parameters in an ASP.NET Core Web API: query strings, path parameters, request body, and request headers. I’ll show examples of these below. Query string parameters Let’s say you have two query string keys: name and servings. To get these query string values, add method parameters with names that … Read more

ASP.NET Core – Logging requests and responses

The simplest way to log requests/responses is to use the HTTP Logging middleware (added in v6). This is configurable, so you can make it suit your needs. If you need more control, you can add your own middleware instead. To use the HTTP Logging middleware, call UseHttpLogging() in your initialization code: Then add Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware to … Read more

ASP.NET Core – How to get request headers

There are two ways to get request headers: When a request comes in, the framework loads request headers into the Request.Headers dictionary. You can use this just like any other dictionary. Here’s an example of using TryGetValue() to check if a request header exists and get its value: Note: To just check if a header … Read more

C# – Handling redirects with HttpClient

HttpClient handles redirects automatically. When you send a request, if the response contains a redirect status code (3xx) and redirect location, then it’ll send a new request to the redirect location. You can turn off this auto-redirect behavior by passing in an HttpClientHandler with AllowAutoRedirect=false. This prevents it from following redirects automatically, and allows you … Read more

C# – Configuring HttpClient connection keep-alive

When you use a single instance of HttpClient to send requests, it keeps connections open in order to speed up future requests. By default, idle connections are closed after 2 minutes, and otherwise will be kept open forever (in theory). In reality, the connection can be closed by the server-side (or other external factors) regardless … Read more

C# – How to read response headers with HttpClient

When you send a request with HttpClient, it returns an HttpResponseMessage. You can read the response headers through the HttpResponseMessage.Headers property: This outputs the response headers: Raw response headers are really just key/value(s) pairs. When the response comes in, the headers are loaded into the Headers property (which is of type HttpResponseHeaders). This parses the … Read more