0

The back story is I have a web api that simply serves as a gateway to invoke external services. The reason for this is I don't want to expose the real business API to external services. So, in this gateway controller, it simply receives the information from the business API, calls external services and returns the response to the client, which is the business API. In my gateway controller I have a POST action that takes a parameter of type HttpContent, something like this:

[Route("api/test")]
    public void Test(HttpContent content)
    {
    }

but the web API infrastructure doesn't know how to serialize and deserialize HttpContent type. Is there a way to support my scenario? Thanks.

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
Nico
  • 497
  • 1
  • 4
  • 15

1 Answers1

0

Accessing the content of the request in any controller's handler is quite natural. Just call Request.Content inside. You can't have it as a parameter because it is not a mapping as an URI segment would nor a serialized content.

Try something like:

using System.Net;
using System.Net.Http;

//  namespace + controller definition...

[Route("api/test")]
public HttpResponseMessage GetTest() //  This is a GET handler
{
    var myRequestContext = Request.Content;

    //  Do something with the content e.g
    if (!myRequestContext.IsMimeMultipartContent())
    {
        Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
    }
}

BTW, assuming you are creating a REST API controller from what you said, don't forget to indicate the verb this handler is mapped to, either by using the [Http<REST_verb>] attribute in System.Web.Http on the handler or by prefixing the handler name by the verb.

Hope that helps!

gfache
  • 606
  • 6
  • 15
  • The problem is, some of the external services accepts GET and some POST, and even the content type might vary too. So I don't see a simple way to forward the request from business API and send to remote external services. – Nico Sep 01 '17 at 16:11
  • You could maybe have a look on the http message handlers [here](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers) and roll yours but that is another story. If this post answers your question, please accept it. – gfache Sep 04 '17 at 15:11