TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
P K
NA
284
60.4k
Cannot sent Post multipart/form-data Httpclient c# API
Dec 16 2020 10:58 PM
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());
//any random ID
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"
);
/*convert string to stream*/
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;
}
}
Reply
Answers (
1
)
generate signature c#
Block clicks with mouse on the form