Last active
October 25, 2018 10:44
-
-
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
This file contains hidden or 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 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