Skip to content

Instantly share code, notes, and snippets.

@vainolo
Last active October 25, 2018 10:44
Show Gist options
  • Save vainolo/ee82da29496206737b6f0202d51df9ca to your computer and use it in GitHub Desktop.
Save vainolo/ee82da29496206737b6f0202d51df9ca to your computer and use it in GitHub Desktop.
Azure Functions – Part 3: Handling HTTP Query GET and POST Requests - Sample Function Code
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;
if (name == null)
{
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
name = data?.name;
}
return name == null ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment