I am trying to send by converting string feed XML to stream multipart/form-data but getting bad request as response. Please suggest correct way to post multipart/form-data as XML or text file data
sample code:
- public async Task<string> PostAPICall(string feed,string clientID, string clientSecret)
- {
- try
- {
- HttpClient client = new HttpClient();
- string uri = Constants.ApiUrl + "v3/feeds?feedType=item&setupType=byMatch";
- string token = await _authentication.GetToken(clientID, clientSecret);
- client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(clientID + ":" + clientSecret)));
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- client.DefaultRequestHeaders.Add("WM_SEC.ACCESS_TOKEN", token );
- client.DefaultRequestHeaders.Add("WM_SVC.NAME", "XXXXXXXXX");
- client.DefaultRequestHeaders.Add("WM_QOS.CORRELATION_ID", System.Guid.NewGuid().ToString());
- client.DefaultRequestHeaders.Add("WM_SVC.VERSION", "1.0.0");
- HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
- MultipartFormDataContent form = new MultipartFormDataContent();
- HttpContent content = new StringContent("fileToUpload");
- form.Add(content, "fileToUpload");
-
- byte[] byteArray = Encoding.ASCII.GetBytes(feed);
- MemoryStream stream = new MemoryStream(byteArray);
- content = new StreamContent(stream);
- content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
- {
- Name = "feeds",
- FileName = "feeds.xml"
- };
- form.Add(content);
- string result = string.Empty;
- var response = client.PostAsync(uri, form).Result;
- if (response.StatusCode == System.Net.HttpStatusCode.OK)
- {
- result = await response.Content.ReadAsStringAsync();
- }
- return result;
- }
- catch (Exception e)
- {
- throw e;
- }
- }