AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

HTTP POST example?

Open suarezafelipe opened this issue 7 years ago • 9 comments

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.

suarezafelipe avatar May 29 '19 04:05 suarezafelipe

As mentioned by @suarezafelipe, I am too waiting for the POST request example.

sharadverma810 avatar May 29 '19 13:05 sharadverma810

@stevejgordon can you create a POST sample?

Rick-Anderson avatar May 29 '19 20:05 Rick-Anderson

I'd like to add my voice to a request for a post example along with collecting an expected response object.

dscott0 avatar May 31 '19 19:05 dscott0

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.

dscott0 avatar May 31 '19 21:05 dscott0

What we need is a simpler sample - for example, calling the web API sample

Rick-Anderson avatar Aug 01 '19 00:08 Rick-Anderson

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

JHeLiu avatar Sep 26 '19 04:09 JHeLiu

@serpent5 any chance you could write the code for this?

Rick-Anderson avatar Jan 21 '20 19:01 Rick-Anderson

Moved to master issue: Make HTTP requests using IHttpClientFactory #16641

Rick-Anderson avatar Jan 21 '20 19:01 Rick-Anderson

@Rick-Anderson Yeah, sure.

serpent5 avatar Jan 21 '20 22:01 serpent5