Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Suggested recipe for reading HTTP headers #94

Open
wraslan opened this issue Nov 16, 2018 — with docs.microsoft.com · 0 comments
Open

Suggested recipe for reading HTTP headers #94

wraslan opened this issue Nov 16, 2018 — with docs.microsoft.com · 0 comments

Comments

Copy link

wraslan commented Nov 16, 2018

Dears,

I would like to suggest adding the following recipe for reading HTTP headers, I created and tested this sample code for a customer who was asking for an example code on how to get the client IP address from the HTTP headers. Think this would be helpful to include with the "HTTP and webhooks" recipes for any customer who are looking for the same:
############################################
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using System.Net.Http;
using System.Net;
using Microsoft.Extensions.Logging;
using System;

namespace FunctionApp1
{
public static class Function1
{
[FunctionName("ReadingRequestHeaders")]
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = null)]HttpRequestMessage req, ILogger log)
{
log.LogTrace("Reading the request headers in HTTP Triggers");
// Read headers
try
{
System.Collections.Generic.IEnumerable<string> data = req.Headers.GetValues("x-forwarded-for");
// Echo request data back in the response
return req.CreateResponse(HttpStatusCode.OK, data);
}catch (Exception ex){
log.LogTrace(ex.Message);
return req.CreateResponse(HttpStatusCode.OK, "x-forwarded-for header is not available when testing Functions locally, Try Publishing the function to Azure then invoke it from there!");
}
}
}
}
############################################


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

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

No branches or pull requests

1 participant