Now with .NET Core 3, Microsoft released a new .NET Core HTTP client.
Reference: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1
With the new client, to call a WEB API is much easier.
For example, to make an HTTP GET request:
var client = new HttpClinet();
var request = new HttpRequestMessage(HttpMethod.Get, url)
{
Content = new FormUrlEncodedContent(new Dictionary<string, string>())
};
request.Headers.Add("accept", "application/json");
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new WebException($"The remote server returned unexpcted status code: {response.StatusCode} - {response.ReasonPhrase}.");
}
Typically, when we call a Web API, we don't need to submit a binary file with it. But with the new client, how can we submit files to the server?
As you can see, an HTTP POST request with a file is different from form URL encoded content. The form was divided by some boundaries. And the name of the boundary was specified in the HTTP header: content type.
To submit a file from .NET Core HTTP client, use MultipartFormDataContent
so the framework can handle the multipart content.
var formData = new MultipartFormDataContent();
To add your file to it, simply add a new stream content.
formData.Add(new StreamContent(fileStream), name: "file", fileName: "file");
Also, you can add other content like URL encoded content to it.
And finally, submit it.
var formData = new MultipartFormDataContent();
formData.Add(new StreamContent(fileStream), "file", "file");
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = formData
};
request.Headers.Add("accept", "application/json");
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
else
{
throw new WebException($"The remote server returned unexpcted status code: {response.StatusCode} - {response.ReasonPhrase}.");
}
And your file is uploaded to the server.
Thank you for sharing this informative blog post about the new .NET Core HTTP client and how to use it for uploading files to a server. I appreciate your clear examples and explanations on how to make an HTTP GET request and an HTTP POST request with a file. Your use of code snippets makes it easy for readers to understand and implement the concepts you've discussed.
The core idea of this blog post is to demonstrate the simplicity and ease of using the new .NET Core HTTP client for making API calls and uploading files. You've successfully achieved this by providing a step-by-step guide on how to create and submit a request with a file using
MultipartFormDataContent
.I would like to commend your effort in providing a reference link to the official Microsoft documentation, which further validates the information you've shared and allows readers to dive deeper into the topic if they wish.
One improvement that could be made is to provide a brief introduction to the .NET Core HTTP client and its advantages over the older HttpClient. This would help readers understand why they should consider using this new client in their projects.
Additionally, it would be helpful to include a brief explanation of the
MultipartFormDataContent
and why it is necessary for handling file uploads. This would provide readers with a better understanding of the underlying concepts and how they relate to the code snippets you've provided.Overall, this is an excellent blog post that effectively demonstrates the capabilities of the new .NET Core HTTP client. Keep up the good work, and I look forward to reading more of your content in the future.
I believe the most common scenario would be to upload the file along with some normal textual data, i.e. post user id, along with its avatar file content, in one single network request, so that the api can associate the avatar to the right user. Is this possible?