How to call post API that using form-data in body message by using MVC client-side.
The API body has TWO attributes in form-data that is message another one is attachments:
I have tried a few methods but its return error due to malformed. This is my code:
var client = new HttpClient();
string APIUrl = "https://mynewuploadapi.azurewebsites.net/api/v1/customers/discount";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwiZXhwIjoxNTkzMzYyMTYwLCJpc3MiOiJQbHVzbWlsZXNBUEkifQ.DC42fI7dmKCTwzXPyrI5vs6Sxp0FMrOgvvr_uECzJ7Q");
client.BaseAddress = new Uri("https://mynewuploadapi.azurewebsites.net/api/v1/customers/discount");
client.Timeout = TimeSpan.FromMilliseconds(1000000);
var converter = new JavaScriptSerializer().Serialize(model);
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("message", @converter),
new KeyValuePair<string, string>("attachements", null)
});
var HTTPcontent = new StringContent(@converter, Encoding.UTF8, "multipart/form-data");
var response = await client.PostAsync(APIUrl, HTTPcontent);
and its worked on postman as the screenshot below:

This is another try on my controller which is also return code:500
#region New Try 26Jun
var newclient = new HttpClient();
newclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwiZXhwIjoxNTkzMzYyMTYwLCJpc3MiOiJQbHVzbWlsZXNBUEkifQ.DC42fI7dmKCTwzXPyrI5vs6Sxp0FMrOgvvr_uECzJ7Q");
newclient.Timeout = TimeSpan.FromMilliseconds(1000000);
var dict = new Dictionary<string, string>
{
{ "message", jsonDeserilizeFromModel},
{ "attachements", null }
};
var values = new FormUrlEncodedContent(dict);
var newresponse = await newclient.PostAsync(APIUrl, values);
newresponse.EnsureSuccessStatusCode();
#endregion
Details postman call below:
URL - https://mynewuploadapi. azurewebsites.com/api/v1/customers/discount
Header:
Authorization : Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwiZXhwIjoxNTkzNDIzNzg2LCJpc3MiOiJQbHVzbWlsZXNBUEkifQ.MWa-pbYneOUKvO0_dVVXVFENwBrpU3eGWc47j1YpcmQ
Body. Method (form-post) message:
{
"tx_type": "Kad Perjalanan",
"applicant_type": "Pekerja/Pelajar",
"reg_type": "Permohonan Baharu",
"ic_type": "NRIC",
"ic_no": "821210-10-1010",
"title": "Encik",
"ic_name": "Ali",
"dob": "1982-12-10",
"gender": "Lelaki / Male",
"mailing_address": "",
"state": "Selangor",
"postcode": "40000",
"city": "Subang",
"email": "ali@plusti.my",
"mobile_no": "01020202020",
"v_make": "Proton",
"v_model": "Saga",
"v_year": "2009",
"v_grant": "12345",
"v_plateno": "BTP2021VC",
"co_name": "PLUSTI",
"co_address": "Tepi PLUSTI Highway",
"co_postcode": "40000",
"co_state": "Selangor",
"co_phone": "0375525251",
"tngcardno": "1234554321",
"tngcard_expiry": null,
"rfidtagno": null,
"v_owner": "Y",
"discount_type":"JPP"
}
Sample screenshot post-form body:

Anyone could please help me regarding this? Thank you
