Skip to main content
Send Web Request

How to Make an HTTP Request with C#

C# uses HttpClient for HTTP requests. It's designed for reuse—create one instance and reuse it across requests.

GET request example

Making a GET request to fetch data with custom headers:

using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Authorization", "Bearer YOUR_TOKEN");
var response = await client.GetAsync("https://api.example.com/users");
var content = await response.Content.ReadAsStringAsync();

POST request example

Making a POST request to send JSON data:

using var client = new HttpClient();
var json = "{"name":"John","email":"john@example.com"}";
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.example.com/users", content);
var result = await response.Content.ReadAsStringAsync();

PUT request example

Replacing a resource with PUT:

var content = new StringContent("{\"name\":\"John Updated\"}", Encoding.UTF8, "application/json");
var response = await client.PutAsync("https://api.example.com/users/123", content);

DELETE request example

Deleting a resource:

var response = await client.DeleteAsync("https://api.example.com/users/123");

Error handling

HttpClient does not throw on 4xx/5xx. Check response.IsSuccessStatusCode before reading the body. Handle HttpRequestException for network errors.

Try it in Send Web Request

Use Send Web Request to test the API before writing C# code. Verify the request/response in the browser.

Open Send Web Request

Other libraries you can try