Skip to content

Suggested recipe for reading HTTP headers #94

Open
@wraslan

Description

@wraslan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions