c# - How to parse form data using Azure Functions -
i trying form data within azure function.
public static async task<httpresponsemessage> run(httprequestmessage req, tracewriter log) { log.info("c# http trigger function processed request."); namevaluecollection col = req.content.readasformdataasync().result; return req.createresponse(httpstatuscode.ok, "ok"); }
i getting following error:
exception while executing function: system.net.http.formatting: no mediatypeformatter available read object of type 'formdatacollection' content media type 'multipart/form-data'.
i trying parse inbound emails via sendgrid described here. https://sendgrid.com/docs/classroom/basics/inbound_parse_webhook/setting_up_the_inbound_parse_webhook.html
the incoming request looks correct.
--xyzzy content-disposition: form-data; name="attachments"
0 --xyzzy content-disposition: form-data; name="text"
hello world --xyzzy content-disposition: form-data; name="subject"
subject --xyzzy content-disposition: form-data; name="to"
based on error message, using multipart/form-data request content type. haven't post media type data server.
if want send plain data server, change content type application/x-www-form-urlencoded , modify request body following format.
name=attachments&anothername=anothervalue
if want form data multi part post, using multipartformdatastreamprovider.
string filepath = "set temp path store uploaded file"; var provider = new multipartformdatastreamprovider(filepath); var multipartprovider = await req.content.readasmultipartasync(provider); var formdata = multipartprovider.formdata;
manually parse content of request body.
string content = await req.content.readasstringasync(); string formdata = content.split(';')[1]; string[] namevalues = formdata.split('&'); namevaluecollection col = new namevaluecollection(); foreach (string item in namevalues) { string[] namevalueitem = item.split('='); col.add(namevalueitem[0], namevalueitem[1]); }
Comments
Post a Comment