Created
March 2, 2017 20:28
-
-
Save syron/066593af4161e3f7218dfca5085c7b72 to your computer and use it in GitHub Desktop.
Implementing etag in Azure Funcion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using syron.ETagHelper; | |
using System.Net; | |
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
// parse query parameter | |
string name = req.GetQueryNameValuePairs() | |
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0) | |
.Value; | |
// Get request body | |
dynamic data = await req.Content.ReadAsAsync<object>(); | |
// Set name to query string or body data | |
name = name ?? data?.name; | |
var responseString = "Hello " + name; | |
var etag = ETagCreator.Create("Hello World"); | |
// get request header | |
IEnumerable<string> values; | |
if (req.Headers.TryGetValues("If-None-Match", out values) && values.First() == etag) | |
{ | |
return req.CreateResponse(HttpStatusCode.NotModified); | |
} | |
var response = req.CreateResponse(HttpStatusCode.OK, responseString); | |
response.Headers.Add("ETag", etag); | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment