NielsSchutte
NielsSchutte

Reputation: 65

How can I print HTTP request header elements in Azure API management?

I'm pretty new to azure API management and I'm trying to check or print the HTTP request header "Host" of my API call in Azure API management with policies.

HTTP header response:

Request headers:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,nl;q=0.6
Cache-Control: max-age=0
Cookie: __utma=124807636.1965218888.1554190818.1554190818.1554190818.1; 
__utmc=124807636; __utmz=124807636.1554190818.1.1.utmcsr=google|utmccn= 
(organic)|utmcmd=organic|utmctr=(not%20provided); __utmt=1; 
__utmb=124807636.4.10.1554190818
Host: api.openweathermap.org
Proxy-Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36

This is what I've tried so far:

<policies>
<inbound>
    <check-header name="Host" failed-check-httpcode="401" failed-check-error-message="Not JSON" ignore-case="false">
        <value>api.openweathermap.org</value>
    </check-header>
    <base />
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <base />
</outbound>
<on-error>
    <base />
</on-error>

Hope someone can help.

Thanks in advance!

Upvotes: 1

Views: 11113

Answers (2)

Shamrai Aleksander
Shamrai Aleksander

Reputation: 16018

You may something like this:

 <set-variable name="HostName" value="@{
            string [] HostNameHeader;
            
            context.Request.Headers.TryGetValue("Host", out HostNameHeader);    

            return HostNameHeader[0];
        }" />

Upvotes: 1

Arup Nayak
Arup Nayak

Reputation: 65

Try Policy expressions in Azure API Management

@(context.Request.Headers.TryGetValue("Host", out value))

For more information refer below link:

API Management policy expressions

Upvotes: 3

Related Questions