Would anyone know how to convert the below Postman POST Body form-data parameters to C# POST request, I tried a lot using HttpClient, Web request at my end but it's not working.
Sample code:
var bytes = (dynamic)null;
using (HttpClient httpClient = new HttpClient())
{
using (var multiPartContent = new MultipartFormDataContent())
{
var fileContent = new ByteArrayContent(documentData);
//Add file to the multipart request
multiPartContent.Headers.Add("format_source", FORMAT_SOURCE);
multiPartContent.Headers.Add("format_target", FORMAT_TARGET);
multiPartContent.Add(fileContent);
//Post it
bytes = httpClient.PostAsync(CONVERSION_URL, multiPartContent).Result;
}
}
Postman code: C# RestSharp:
var client = new RestClient("http://test-service-staging.test.com/convert");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddFile("input", "/C:/Users/DR-11/Downloads/response_1592912235925.xml");
request.AddParameter("format_source", "abc");
request.AddParameter("format_target", "xyz");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
HTTP:
POST /convert HTTP/1.1
Host: test-service-staging.test.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="input"; filename="/C:/Users/DR-11/Downloads/response_1592912235925.xml"
Content-Type: text/xml
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="format_source"
abc
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="format_target"
xyz
----WebKitFormBoundary7MA4YWxkTrZu0gW
Curl:
curl --location --request POST 'http://test-service-staging.test.com/convert' \
--form 'input=@/C:/Users/DR-11/Downloads/response_1592912235925.xml' \
--form 'format_source=abc' \
--form 'format_target=xyz'
Instead of file path, need to pass the bytes in the API.