Skip to content

Instantly share code, notes, and snippets.

@vainolo
Last active October 25, 2018 10:47
Show Gist options
  • Save vainolo/8d37cc248f307847be4227dffd28c417 to your computer and use it in GitHub Desktop.
Save vainolo/8d37cc248f307847be4227dffd28c417 to your computer and use it in GitHub Desktop.
Azure Functions – Part 3: Handling HTTP Query GET and POST Requests - GET and POST handling code
#r "Newtonsoft.Json"using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string name = null;
if (req.Method == HttpMethod.Post)
{
log.Info($"POST method was used to invoke the function");
string body = await req.Content.ReadAsStringAsync();
dynamic b = JObject.Parse(body);
name = b.name;
}
else if (req.Method == HttpMethod.Get)
{
log.Info($"GET method was used to invoke the function");
name = req.GetQueryNameValuePairs().ToDictionary(x => x.Key, x => x.Value)["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