The call with bearer token to get XML with XML argument is done so far with RestSharp
where
XElement Payload = <Access><AppID>35</AppID><UserToken>QrEil9L9S</UserToken></Access>
:
public string PostFromRest(string API_URL, string subURL, string bearerToken, XElement Payload)
{
var client = new RestClient(API_URL);
var request = new RestRequest(subURL, Method.Post);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/xml");
request.AddHeader("Accept", "application/xml");
request.AddHeader("messageType", "REQUEST");
request.AddHeader("Authorization", bearerToken);
request.AddHeader("messageId", Guid.NewGuid().ToString());
request.AddHeader("timestamp", DateTime.Now.ToString());
request.AddXmlBody(Payload, ContentType.Xml);
var response = client.Execute(request);
return response.Content;
}
I need to do the same with HttpClient. I was trying:
HttpClient client = new HttpClient())
client.DefaultRequestHeaders.Clear();
client.BaseAddress = new Uri(API_URL);
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Authorization", bearerToken);
var content1 = new FormUrlEncodedContent(
new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("Cache-Control", "no-cache"),
new KeyValuePair<string, string>("Content-Type", "application/xml"),
new KeyValuePair<string, string>("Accept", "application/xml"),
new KeyValuePair<string, string>("messageType", "REQUEST"),
new KeyValuePair<string, string>("messageId", Guid.NewGuid().ToString()),
new KeyValuePair<string, string>("timestamp", DateTime.Now.ToString())});
HttpResponseMessage response = client.PostAsync(subURL, content).Result;
But authentication is failed. I was trying to use HttpRequestMessage:
using (var request = new HttpRequestMessage(HttpMethod.Post, API_URL))
{
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Content-Type", "application/xml");
request.Headers.Add("Accept", "application/xml");
request.Headers.Add("messageType", "REQUEST");
request.Headers.Add("Authorization", bearerToken);
request.Headers.Add("messageId", Guid.NewGuid().ToString());
request.Headers.Add("timestamp", DateTime.Now.ToString());
request.Content = new StringContent(Payload.ToString(), Encoding.UTF8, "application/xml");
HttpResponseMessage response = client.SendAsync(request).Result;
}
But authorization is failed too. How to call HttpClient with bearer token to get XML with XML argument?
Here is one way to achieve the same with vanilla HttpClient
:
HttpClient
instance and set its BaseAddress
var client = new HttpClient() { BaseAddress = new Uri(API_URL) };
HttpRequestMessage
and set the relative URL and the verbvar request = new HttpRequestMessage(HttpMethod.Post, new Uri(subURL));
//Standard headers
request.Headers.Add("Cache-Control", "no-cache");
request.Headers.Add("Accept", "application/xml");
request.Headers.Add("Authorization", bearerToken);
//Custom headers
request.Headers.Add("messageType", "REQUEST");
request.Headers.Add("messageId", Guid.NewGuid().ToString());
request.Headers.Add("timestamp", DateTime.Now.ToString());
var content = new StringContent(Payload.ToString(), Encoding.UTF8, "application/xml");
request.Content = content;
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();