Reputation: 13
I want to set the Content-Type and Authorization Headers using HttpClient and want to load the xml file for the body(request) and send Post request.
I have searched for combining both headers and body for Post request.
Setting up the Headers using HttpRequestMessage.
HttpClient clientTest = new HttpClient();
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post,url);
httpRequest.Content = new StringContent(string.Empty, Encoding.UTF8, "application/vnd.citrix.sessionparams+xml");
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("CitrixAuth", "H4sIAAAAAAAEAK1X2Y6jyBL9lZLnEbnZt1J");
Setting the xml body -
XDocument xml = XDocument.Load("RequestSession.xml");
Converting to Document.ToString()
var httpContent = new StringContent(xml.Document.ToString(), Encoding.UTF8, "application/vnd.citrix.sessionstate+xml");
Please help me with combining both headers and body for Post Request using HttpClient.
Upvotes: 1
Views: 10679
Reputation: 11514
I do not know if this is a valid request for the service you are calling, but to set headers and body and send a request it would look like this:
HttpClient clientTest = new HttpClient();
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post,url);
httpRequest.Content = new StringContent(xml.Document.ToString(), Encoding.UTF8, "application/vnd.citrix.sessionstate+xml");
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("CitrixAuth", "H4sIAAAAAAAEAK1X2Y6jyBL9lZLnEbnZt1J");
var response = await clientTest.SendAsync(httpRequest);
If you get a 400 Bad Request it because what you are sending is not appropriate for the API, not because of how you are sending it.
Upvotes: 0