Bad request: Error parsing form field

We were calling the https://api.cloudflare.com/client/v4/accounts/:account_tag/images/v1 image upload API using .NET Core with C# and kept getting a 400 Bad Request error with an error message in the response reading “Bad request: Error parsing form field”. It took a bit of sleuthing and we found that when .NET creates a multipart form it adds a Content-Disposition header similar to the following:

Content-Disposition: form-data; name=file; filename*=utf-8’'myimage.png

Note that this is different than what CURL produces:

Content-Disposition: form-data; name=“file”; filename=“myimage.png”

Initially we thought the UTF-8 notation was tripping things up but it turned out to be something far more simple. It appears that the upload API expects the name and filename values it be enclosed in quotes.

The simple fix for us was to just include the quotes as part of the text when defining the file and filename values:

var multipartContent = new MultipartFormDataContent();
multipartContent.Add(imageBinaryContent, “\“file\””, “\“myimage.png\””);

Instead of what we had before which was just:

var multipartContent = new MultipartFormDataContent();
multipartContent.Add(imageBinaryContent, “file”, “myimage.png”);

I hope this helps anyone who might be experiencing the same issue.

1 Like

Lifesaver! Thank you.

I can’t mark first posts as a Solution, but this sounds like a fix.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.