S
S
StelX2018-02-15 15:40:13
C++ / C#
StelX, 2018-02-15 15:40:13

C#. How to fix error 415 Unsupported Media Type?

I started writing applications that interact with various APIs. Requests checked in the browser and in the application. GET requests to one of the servers in the application return a 415 Unsupported Media Type error . Other servers don't have this problem. Chrome gets a response.
On advice from google, I tried to add request.ContentType = "application/json" , but there was no result.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.ContentType = "application/json";
            
            Stream dataStream;
            HttpWebResponse WebResponse = (HttpWebResponse)request.GetResponse(); //  *(415) Unsupported Media Type.
            dataStream = WebResponse.GetResponseStream();
            StreamReader StreamReader = new StreamReader(dataStream);
            ResponseFromServer = StreamReader.ReadToEnd();
            dataStream.Close();
            WebResponse.Close();
            StatusCode = HttpStatusCode.OK;

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
StelX, 2018-02-15
@StelX

Here it helped

request.AutomaticDecompression = DecompressionMethods.GZip;

O
Oleg Pogrebnyak, 2018-02-15
@S0HardCore

Tryrequest.Accept

S
Stanislav Makarov, 2018-02-15
@Nipheris

What are you betting Content-Typeon in a GET request? This header is set by the side that sends the body - either the server, when sending a response with a body, or the client, when sending a request with a body (ie POST or PUT). As Oleg Pogrebnyak
already pointed out , you need an Accept header .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question