Here is code I came up with, based on yours:
static async void GoPost()
{
string baseUrl = "http://localhost" + "/api/Payment/AddMedicineOrder";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("username", "benemanuel");
parameters.Add("FullName", "benjamin emanuel");
parameters.Add("Phone", "0800-0800000");
parameters.Add("CNIC", "1234");
parameters.Add("address", "an address");
parameters.Add("Email", "ben@benemanuel.net");
parameters.Add("dateofbirth", "14/05/1974");
parameters.Add("Gender", "Male");
parameters.Add("PaymentMethod", "Credit Card");
parameters.Add("Title", "Mr");
parameters.Add("PhramaList", "123");
HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new StringContent("fileToUpload");
HttpContent DictionaryItems = new FormUrlEncodedContent(parameters);
form.Add(content, "fileToUpload");
form.Add(DictionaryItems, "medicineOrder");
var stream = new FileStream("c:\\TemporyFiles\\test.jpg", FileMode.Open);
content = new StreamContent(stream);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "fileToUpload",
FileName = "AFile.txt"
};
form.Add(content);
HttpResponseMessage response = null;
try
{
response = (client.PostAsync("http://localhost:54169/api/Values/AddMedicineOrder?username=ben", form)).Result;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
var k = response.Content.ReadAsStringAsync().Result;
}
and WebApi:
[Route("api/Values/AddMedicineOrder")]
[HttpPost]
public HttpResponseMessage AddMedicineOrder(string username)
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
return Request.CreateResponse(HttpStatusCode.InternalServerError);
var request = HttpContext.Current.Request;
bool SubmittedFile = (request.Files.Count != 0);
string vals = request.Form.GetValues("medicineOrder").First();
NameValueCollection vals2 = HttpUtility.ParseQueryString(vals);
string value1 = vals2.GetValues("username").First();
string value2 = vals2.GetValues("FullName").First();
int count = vals2.Count;
return Request.CreateResponse(HttpStatusCode.OK);
}
This only gets the dictionary values you wanted, I haven't tried to extract the file from the stream or anything else, I'll leave that up to you :)