HTTP POST example?
This page is ranking # 2 on google for "send http post request in .net core" and yet it does not contain a single example on how to send a POST request.
I understand this might not be the place to put such documentation, but I haven't been able to find a single post request example in the .net core official docs.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 881cef77-11ef-ac11-06d0-0996a1ccfa1b
- Version Independent ID: 3d356334-e672-7e8b-1fef-7944ebee99eb
- Content: Make HTTP requests using IHttpClientFactory in ASP.NET Core
- Content Source: aspnetcore/fundamentals/http-requests.md
- Product: aspnet-core
- Technology: aspnetcore-fundamentals
- GitHub Login: @stevejgordon
- Microsoft Alias: scaddie
As mentioned by @suarezafelipe, I am too waiting for the POST request example.
@stevejgordon can you create a POST sample?
I'd like to add my voice to a request for a post example along with collecting an expected response object.
I finally managed it. The following controller action uses a named HttpClient to post two parameters (clientId and secret) to a remote API in order to request a jwt token as the response. The parameters are each strings and they were posted as a single JSON object which contained them.
//first the dependency injection
private readonly IHttpClientFactory _httpClientFactory;
public PlatformController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
//controller action
public async Task<ActionResult<string>> RequestToken()
{
string secretString = "*****";
string clientIdString = "*****";
var result = await _httpClientFactory.CreateClient("RequestTokenClient").PostAsJsonAsync("api/v1/clientauthentication", new { clientId = clientIdString, secret = secretString });
return await result.Content.ReadAsStringAsync();
}
Hope that helps someone else! This is actually very useful now that it is going. I will move this method out of the controller and use it as a service that supplies a token on request, and it will only require a single HttpClient be generated to do so. Pretty neat.
What we need is a simpler sample - for example, calling the web API sample
I'm also looking forward to having one. Will it be used like.net before?Not dependency injection, because we're going to write a generic library that can be used like.net before
public HttpClient HttpClient()
{
return HttpClientFactory.Create();
}
need an example
@serpent5 any chance you could write the code for this?
Moved to master issue: Make HTTP requests using IHttpClientFactory #16641
@Rick-Anderson Yeah, sure.