ilsologheo00
ilsologheo00

Reputation: 57

Unsupported media type error with application/json specified

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

Answers (1)

Ramp2010
Ramp2010

Reputation: 76

Try removing the slashes and double quotes.

            string json = "{'params': {" +
                       "'companyId':'" + bitDefenderCompanyId + "'}," +
                       "'id': '" + bitDefenderCompanyId + "'," +
                       "'jsonrpc': '2.0'," +
                       "'method': 'getLicenseInfo'}";

Upvotes: 0

Related Questions