0

I have an asp.net webapi application that uses some external services in the backend, is there a way to add a custom header to all outbound request (like rest or soap reqests)? I can create a filter but it only intercepts incoming requests and responses of my webapi clients.

CaTourist
  • 711
  • 3
  • 9
  • 21
  • 1
    One way is to create a wrapper over HttpClient and use this wrapper to call your 3rd party services. You could configure headers inside the wrapper before making the calls. Just a thought, there might be more elegant solutions. – Developer Nov 24 '16 at 17:46
  • Please find the answer in the below link; http://stackoverflow.com/a/22998513/705947 – c-sharp Nov 24 '16 at 18:11
  • thank you but i don't need to add headers to soap requests but to all http requests without changing every client component on web application – CaTourist Nov 25 '16 at 13:33
  • Do you have any sample code for how you make requests to those other services? I think that @Developer likely has the best solution for you. – Prescott Nov 27 '16 at 02:46

1 Answers1

0

Two possible solutions for your problem:

  1. Create custom versions of your http request modules (HttpClient, HttpWebRequest, ...), sign the assemblies and register then on the GAC, an example is available in https://stackoverflow.com/a/9266821/5270073.

The bad point of this approach is that every time that a developer uses a new module to consume an external service, you need to implement and register this custom new module.

  1. Add an API gateway, or an proxy, in your infrastructure (Kong, Tyk, ...), register your external services in this gateway and consume the external services from API gateway. You could easily add an custom header to all requests with this network component.

The flow:

api.mydomain.com => externalservice.com

Changes to:

api.mydomain.com => apigateway.mydomain.com/externalservice => externalservice.com

Personally I prefer the second option because:

  • You can control the consume of services;
  • You can add custom headers to another applications ou servers;
  • This approach meets as DevOps practices.

Customize requests on Kong: https://getkong.org/plugins/request-transformer/

Customize requests on Tyk: https://tyk.io/tyk-documentation/transform-traffic/request-method-transform/

Community
  • 1
  • 1
Ricardo Fontana
  • 4,583
  • 1
  • 21
  • 32