C# post API endpoint returning Bad request but works from Postman

Viewed 33

I am trying to Call a Post API with basic authentication and a XML payload. I am able to make the call successfully from Postman with the following details:

Post API Url:  https://somedomain.com/WebServices/webservice.aspx?PostODMClinicalData
Authentication type:  basic (user id and password provided in postman)
content type: application/xml
payload:  <My XML content>

In postman, I am able to receive the response correctly. However I need to make the call from a .Net core 3.1 app. This is the code I have tried.

var Url = "https://somedomain.com/WebServices/webservice.aspx?PostODMClinicalData";
var Content = new StringContent(MyXMLString, Encoding.UTF8, "application/xml");

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("ContentType", "application/xml");
var plainTextBytes = Encoding.UTF8.GetBytes("MyUserName:MyPassPhrase");
string val = Convert.ToBase64String(plainTextBytes);
httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + val);
HttpResponseMessage response = httpClient.PostAsync(Url, Content).Result;

I have double checked, the XML string is perfect. But I am getting bad request. I guess it's because of the ?PostODMClinicalData part in the URL ?

How can I make the post call correctly from C# code?

0 Answers
Related