Reputation: 57
I am trying to access a public API with a POST Method and with the following JSON body:
{
"params": {
"companyId":"620e91a211b42421733aa2b4"
},
"id": "620e91a211b42421733aa2b4",
"jsonrpc": "2.0", "method": "getLicenseInfo"
}
Which correctly returns the expected values in Postman. Unfortunately I have a problem while sending the request via a C# asp.net application since after using the following code:
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, GlobalFunctions.GetBitDefenderBaseURL() + "/licensing");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("cache-control", "no-cache");
request.Headers.Add("Connection", "keep-alive");
request.Headers.Add("user-agent", "ReservedArea/1.0");
string base64Token = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(GlobalFunctions.GetBitDefenderAPIKey() + ":"));
request.Headers.Add("Authorization","Basic " + base64Token );
string json = "{\"params\": {" +
"\"companyId\":\"" + bitDefenderCompanyId + "\"}," +
"\"id\": \"" + bitDefenderCompanyId + "\"," +
"\"jsonrpc\": \"2.0\"," +
"\"method\": \"getLicenseInfo\"}";
var httpContent = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");
request.Content = httpContent;
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
I get an "Usupported media type error".
As far as I understood I am correctly specifying the request content-type correctly while creating the StringContent object, but I am having no luck in getting a correct response from the API I am trying to contact.
Many thanks to anyone who is eager to help me.
Upvotes: 0
Views: 956
Reputation: 76
Try removing the slashes and double quotes.
string json = "{'params': {" +
"'companyId':'" + bitDefenderCompanyId + "'}," +
"'id': '" + bitDefenderCompanyId + "'," +
"'jsonrpc': '2.0'," +
"'method': 'getLicenseInfo'}";
Upvotes: 0