To request JSON from a URL using C#/.NET, you need to send an HTTP GET request to the server and provide the Accept: application/json request header with your request. The Accept header tells the server that our C#/.NET client is expecting JSON. The server informs the C#/.NET client that it has returned JSON with a Content-Type: application/json response header. In this C#/.NET request JSON example, we are making a GET request to the ReqBin echo URL. The C#/.NET code was automatically generated for the GET JSON example.
How to request data in JSON format using C#/.NET?
To get data in JSON format from the server, the C#/.NET client must explicitly tell the server that it expects JSON by sending the Accept: application/json request header. Without the Accept header, the server may automatically send data in a different format that it thinks is best for the API client (based on the UserAgent header), and it might not be JSON.
JSON Accept Header Example
C#/.NET JSON Request Example
An example of an HTTP GET request to fetch JSON data from a ReqBin echo URL. Click Generate Code to see the C#/.NET code for this JSON request example.
JSON Request Example
GET /echo/get/json HTTP/1.1
The server response to our C#/.NET client's request.
JSON Response Example
HTTP/1.1 200 OK
The server returned JSON in the body of the HTTP response message. The Content-Length: 19 header indicates the length of the JSON data in the response body, and the Content-Type: application/json response header indicates the type of data.
JSON MIME Type
The official MIME type for JSON is application/json: