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# – Examples of using JsonDocument to read JSON

You can use the JsonDocument class when you want to read and process JSON without having to deserialize the whole thing to an object. For example, let’s say you have the following JSON object representing wind variables: Now let’s say you’re only interested in the wind speed. Instead of having to deserialize this into a … Read more

C# – Deserialize JSON as a stream

Here’s an example of deserializing a JSON file as a stream with System.Text.Json: Stream deserialization has three main benefits: It’s memory-efficient, which improves overall performance. Fail fast when there’s a problem in the JSON data. Deserialization process can be canceled (async version only). In this article, I’ll go into details about these benefits and show … Read more

C# – Configuring how long an HttpClient connection will stay open

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# – Disposing the request HttpContent when using HttpClient

Before .NET Core 3.0 (including .NET Framework), HttpClient disposes the request HttpContent object for you. This is surprising default behavior (a violation of the principle of least surprise for sure). This causes multiple problems, but one of the main problems is it prevents you from reusing the HttpContent object (you’re greeted with an ObjectDisposedException if … Read more

ASP.NET Core – How to receive a file in a web API request

When the client posts a file in a multipart/form-data request, it’s loaded into an IFormFile object. This contains file information (such as the file name) and exposes the file content as a stream. This allows you to save the file or process it however you want to. You can access the IFormFile object through Request.Form.Files: … Read more

JavaScript – Post form data to a web API asynchronously

You can post form data asynchronously by using fetch() + FormData, like this: FormData parses the form element into an array of key/value pairs for you. This results in sending the following request: When you set the request body to a FormData object, it sets the content-type to multipart/form-data. You can post the data in … Read more

JavaScript – FormData initialized from a form element is empty

Problem When you try to initialize a FormData object from a form element, it’s empty. There are no keys or values whatsoever. For example, let’s say you have a form called movieForm and you’re trying to post its data with fetch(): When you look at the request, it has the right Content-Type, but no data: … Read more

JavaScript – How to send a request to a web API

The simplest way to send a request to a web API with JavaScript is to use fetch(). Here’s an example that gets a stock quote from a web API and displays the results: You can wire this up to a button click (and have a div for displaying the results): Note: fetch() is supported in … Read more

C# – How to cancel an HttpClient request

It’s a good idea to provide users with a way to cancel a HttpClient request that’s taking too long. To be able to cancel an HttpClient request, you can pass in a CancellationToken: To get a CancellationToken, you have to create a CancellationTokenSource: To actually cancel the request, you have to call CancellationTokenSource.Cancel(): This means … Read more