Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP POST example? #12580

Closed
suarezafelipe opened this issue May 29, 2019 — with docs.microsoft.com · 9 comments · Fixed by #18650
Closed

HTTP POST example? #12580

suarezafelipe opened this issue May 29, 2019 — with docs.microsoft.com · 9 comments · Fixed by #18650
Labels
doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue
Milestone

Comments

Copy link

suarezafelipe commented May 29, 2019

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.

@dotnet-bot dotnet-bot added the Source - Docs.ms Docs Customer feedback via GitHub Issue label May 29, 2019
Copy link

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

@Rick-Anderson
Copy link
Contributor

@stevejgordon can you create a POST sample?

@Rick-Anderson Rick-Anderson added this to the Backlog milestone May 29, 2019
Copy link

dscott0 commented May 31, 2019

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

@dscott0
Copy link

dscott0 commented May 31, 2019

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.

@Rick-Anderson
Copy link
Contributor

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

@JHeLiu
Copy link

JHeLiu commented Sep 26, 2019

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

@Rick-Anderson
Copy link
Contributor

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

@Rick-Anderson
Copy link
Contributor

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

@serpent5
Copy link
Contributor

@Rick-Anderson Yeah, sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants