0

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.

5
  • Is client HttpClient? Commented Nov 11, 2022 at 12:22
  • Yes, the client is HTTP Commented Nov 11, 2022 at 13:26
  • There are other overloads on http client like PostJsonAsync. Could you give that a try. It is much simpler and requires less code, which is always better. Commented Nov 14, 2022 at 7:47
  • I tried, but I still got the same error. Commented Nov 14, 2022 at 16:23
  • Do you mind to record the request with a tool like Fidller so we can see what the raw request looks like? Commented Nov 15, 2022 at 7:58

1 Answer 1

0

Try removing the slashes and double quotes.

            string json = "{'params': {" +
                       "'companyId':'" + bitDefenderCompanyId + "'}," +
                       "'id': '" + bitDefenderCompanyId + "'," +
                       "'jsonrpc': '2.0'," +
                       "'method': 'getLicenseInfo'}";
Sign up to request clarification or add additional context in comments.

3 Comments

Done, but still I've got the same error
Try using Newtonsoft to convert to httpcontent and send. See if that works. var jsonConverted = JsonConvert.SerializeObject(json, Formatting.Indented); var httpContent2 = new StringContent(jsonConverted);
Already used Newtosoft, but sill I got the unsupported media

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.