6

How to read form data in Azure functions? I tried to do it in several ways, but always I get an error, eg.:

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    dynamic data = await req.Content.ReadAsFormDataAsync();

    return req.CreateResponse(HttpStatusCode.OK, $" {data}");
}

Error: Exception while executing function: Functions.FormTrigger. System.Net.Http.Formatting: No MediaTypeFormatter is available to read an object of type 'FormDataCollection' from content with media type 'application/json'.

I checked request content and I'm getting request as multipart/form-data:

" ------WebKitFormBoundary47wKq7pk9Fcc4H9J\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nsdgs\r\n
------WebKitFormBoundary47wKq7pk9Fcc4H9J\r\nContent-Disposition: form-data; name=\" _replyto\"\r\n\r\nsdg@sdg.com\r\n
------WebKitFormBoundary47wKq7pk9Fcc4H9J\r\nContent-Disposition: form-data; name=\"message\"\r\n\r\nsdgsd\r\n
------WebKitFormBoundary47wKq7pk9Fcc4H9J--\r\n"

Thank you for any hints.

Thomas
  • 24,234
  • 6
  • 81
  • 125
Pawel Maga
  • 5,428
  • 3
  • 38
  • 62
  • 1
    What is the type of request content? I.e., whether it is - application/x-www-form-urlencoded, or do you have application/json contents? "ReadAsFormDataAsync" only accepts "application/x-www-form-urlencoded" type of contents. – Nirman Dec 15 '16 at 09:31
  • @Nirman I tried it and it works, but always get empty result from expressions, like: data["message"] – Pawel Maga Dec 15 '16 at 09:47
  • Can you try reading - req.Content.ReadAsFormDataAsync().Result ? This should give you name-value collection – Nirman Dec 15 '16 at 09:57
  • @Nirman It returns me something like that: http://pastebin.com/vnKsQQCb :( I think it's strongly related with Azure Functions... – Pawel Maga Dec 15 '16 at 10:02
  • seems strange! one last go from my side - NameValueCollection col = req.Content.ReadAsFormDataAsync().Result; return req.CreateResponse(HttpStatusCode.OK, $" {col[0]}"); – Nirman Dec 15 '16 at 10:07
  • 1
    @Nirman I figured out what is a problem. Your first comment was correct... I tested it using Postman and form data were in `form-data` section instead of `x-www-form-urlencoded` section. Unfortunately my header was ignore. Post it as answer and I'll mark it a an answer. Thanks a lot! – Pawel Maga Dec 15 '16 at 10:13

2 Answers2

9

As the request contains "application/x-www-form-urlencoded" type of contents, you need to convert the input to NameValueCollection in order to read input:

NameValueCollection col = req.Content.ReadAsFormDataAsync().Result; 
return req.CreateResponse(HttpStatusCode.OK, $" {col[0]}");

You can also pass Key string instead of Index which would make the code more readable and self-explanatory

Nirman
  • 6,715
  • 19
  • 72
  • 139
1

In order to read form data, there is the method ReadAsMultipartAsync:

data = await req.Content.ReadAsMultipartAsync().ConfigureAwait(false);
Majix
  • 570
  • 7
  • 14