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

Post Requests? #82

Open
DavidBrower opened this issue Aug 30, 2021 · 2 comments
Open

Post Requests? #82

DavidBrower opened this issue Aug 30, 2021 · 2 comments

Comments

@DavidBrower
Copy link

I've been puzzling how to use AspNetCore.Proxy to forward a Post request to a Web API Controller. Currently, the code looks like this:

   [HttpPost]
    [Route("[action]")]
    public async Task<ActionResult> Login(LoginViewModel model)
    {
        //return ProxyTo(serverUrl, LoginUri, model);
        HttpClient httpClient = new HttpClient();
        HttpResponseMessage response = await httpClient.PostAsync(serverUrl + LoginUri, new JsonContent(model));
        return Ok(response);
    }

How would I rewrite this to use AspNetCore.Proxy?

@twitchax
Copy link
Owner

twitchax commented Sep 7, 2021

Hi @DavidBrower, sorry for the delay: I fractured my clavicle, and was out for a bit.

You don't need to return a response: the proxy function does everything for you. Check out this sample.

[Route("api/google/{**rest}")]
public Task ProxyCatchAll(string rest)
{
    // If you don't need the query string, then you can remove this.
    var queryString = this.Request.QueryString.Value;
    return this.HttpProxyAsync($"https://google.com/{rest}{queryString}");
}

@joaocpribeiro
Copy link

For swagger purposes, I wanted to maintain the content model in the endpoint definition. Therefore I could manage to forward the content with the following code:

var httpProxyOptions = HttpProxyOptionsBuilder.Instance
                .WithBeforeSend((c, hrm) =>
                {
                    hrm.Content = new StringContent(JsonConvert.SerializeObject(requestModel), Encoding.UTF8, "application/json");
                    return Task.CompletedTask;
                })
                .Build();

I hope this helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants