4

I am having issues constructing the POST request with form-data in C# using HTTPClient. i tried multiple approaches but nothing seems to work. Can you please help what I am missing?

Please be informed, the SWAGGER POST is working fine and based on that I am trying to construct the request.

The API parameters

rPath* string
(formData)
nName* string
(formData)
nFile string
(formData)
type* string
(formData)  
zFile file
file

The working swagger POST

curl -X POST "http://localhost:8888/server/api/v1.0/createfolder" -H "accept: text/plain; charset=UTF-8" -H "authorization: Basic XXXXXXX" -H "Content-Type: multipart/form-data" -F "rPath=/RootFolder/" -F "nName=testing" -F "type=Folder"

The Fiddler request header of the SWAGGER POST (This is working).

Fiddler Request details

    static void Main(string[] args)
    {
        var formData = new MultipartFormDataContent();
        HttpContent content = new FormUrlEncodedContent (new[]
        {
            new KeyValuePair<string, string>("rPath",  "/RootFolder/"),
            new KeyValuePair<string, string>("nName", "TestFolder"),
            new KeyValuePair<string, string>("type", "Folder")
        });           
        content.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
        content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
         formData.Add(content);
        var url = "http://localhost:8888/server/api/v1.0/createfolder";
        HttpResponseMessage response = PostRoot(formData, url);
    }
    static HttpResponseMessage PostRoot(HttpContent content, string webMethod)
    {
        HttpResponseMessage response = new HttpResponseMessage();
        using (var client = new HttpClient() {  })
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "xxxxxxx=");
            response = client.PostAsync(webMethod, content).Result;
        }

        return response;
    }
Abhishek Vyas
  • 599
  • 1
  • 9
  • 24

1 Answers1

4

Here's an example of how to send multi part form data:

var client = new HttpClient();
var baseUrl = "https://someurl";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");

var formContent = new MultipartFormDataContent
{
    {new StringContent("ParamValue1"),"ParamName1"},
    {new StringContent("ParamValue2"),"ParamName2"},
    {new StringContent("ParamValue2"),"ParamName3"},
};

var response = client.PostAsync(baseUrl, formContent).Result;
response.EnsureSuccessStatusCode();

var result = string.Empty;
if (response.IsSuccessStatusCode)
{
    result = response.Content.ReadAsStringAsync().Result;
}
return result;
KyleMit
  • 30,350
  • 66
  • 462
  • 664
tally2512
  • 61
  • 4
  • 2
    Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Ran Marciano Mar 19 '21 at 15:08
  • `"ParamName"` vars may be written with quotes: `"\"ParamName1\""` – S.Serpooshan Jun 02 '23 at 19:08