twitchax / AspNetCore.Proxy

ASP.NET Core Proxies made easy.
MIT License
525 stars 83 forks source link

trying to get my apigateway to work. #36

Closed bodamithun closed 4 years ago

bodamithun commented 4 years ago

I have implemented a webapi with aspnetcore.proxy 4.0, all I need my api to do is whatever request It gets I need it to forward that to the another api and what ever response I get back, I need to return it. so I created two api's, one with aspnetcore.proxy and other one is normal api. one ask is the forwarding request should include all the headers and also all the body(raw json data).

I am using postman to test the apigateway, in the headers

image image

the web api using dotnet core 3.0 has the implementation of aspnetcore.proxy, I have an endpoint as below. this is running on 5002

 [ApiController]
[Route("api/")]  
public class ShippingController : ControllerBase
{
    private readonly ILogger<ShippingController> _logger;
    public ShippingController(ILogger<ShippingController> logger)
    {
        _logger = logger;
    }

    [HttpGet]        
    [Route("[controller]/{**url}")]
    public Task Get(string url)
    {
         var options =  HttpProxyOptionsBuilder.Instance
        .WithShouldAddForwardedHeaders(true).Build();
        return this.ProxyAsync($"https://localhost:5001/{url}","", options);
    }
}

I created another rest api with endpoint. this is running 5001

     [HttpGet]
    [Route("getImagePdf")]
    public async Task<ActionResult<ImageDetails>> GetPdf(ImagePdfRequest imagePdfRequest)
    {
        if(imagePdfRequest == null)
        {
            return BadRequest();
        }
        var response = await _personalizationLogic.GetImageAsync(imagePdfRequest);
        if (response == null) { return NotFound(); }
        return Ok(response);
    }

Now Locally when i testing this using postman, I am seeing the following error { "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13", "title": "Unsupported Media Type", "status": 415, "traceId": "|8b5e160c-47410df4b69e21ac.1.1d8faa92_" }

also other questioni have is whats the way to forward the body(the json data that I was sent in the orginal request) in my request to 5001.

Thanks

bodamithun commented 4 years ago

Also how to forward Json data in the body

twitchax commented 4 years ago

Thanks for the great explanation.

The body is automatically forwarded, so no problem there.

My guess is that 5002 needs an ASP.NET setting to allow the media type that you are forwarding. What is the media type of ImagePdfRequest? Json?

twitchax commented 4 years ago

Also, can you debug 5001? I wonder if the request is actually getting rejected by 5001? Does the request ever get there?

bodamithun commented 4 years ago

Thanks @twitchax for responding, the media type is Json. also i tried putting a break point on 5001, but that never got hit.

Also in the startup.cs

I just have this statement in configureservices method. services.AddProxies();

Am I missing any configuration.

bodamithun commented 4 years ago

Also this is the error i am able to see in the console

System.Net.Http.HttpClient.AspNetCore.Proxy.HttpProxyClient.LogicalHandler: Information: End processing HTTP request after 1304.7942ms - UnsupportedMediaType info: System.Net.Http.HttpClient.AspNetCore.Proxy.HttpProxyClient.LogicalHandler[101] End processing HTTP request after 1304.7942ms - UnsupportedMediaType System.Net.Http.HttpClient.AspNetCore.Proxy.HttpProxyClient.LogicalHandler: Trace: Response Headers: Date: Tue, 03 Mar 2020 23:05:55 GMT Server: Kestrel Content-Type: application/problem+json; charset=utf-8 Content-Length: 158

twitchax commented 4 years ago

Let me try writing a test that will cover this scenario and see what I uncover.

Something fishy, for sure.

bodamithun commented 4 years ago

@twitchax Thank you... did you find anything.

I am thinking that its not able to parse/accept a Json object if you are passing it in the body. But curious to see what you find...

bodamithun commented 4 years ago

@twitchax

I think I got this to work... I had the endpoint set to [HttpGet], so changing it to HttpPost got it to work. Thanks for looking into it. I will close this issue.

twitchax commented 4 years ago

Ohhhhhh, I didn't even notice that, haha.

Yeah, GETs cannot have a body.