HttpClient.PutAsync Method

System.Net.Http

Sends an asynchronous PUT request to the specified resource.

Syntax


public Task<HttpResponseMessage> PutAsync(
    string requestUri,
    HttpContent? content
)

public Task<HttpResponseMessage> PutAsync(
    string requestUri,
    HttpContent? content,
    CancellationToken cancellationToken
)

public Task<HttpResponseMessage> PutAsync(
    Uri? requestUri,
    HttpContent? content
)

public Task<HttpResponseMessage> PutAsync(
    Uri? requestUri,
    HttpContent? content,
    CancellationToken cancellationToken
)
        

Parameters

Name Type Description
requestUri string or Uri The Uniform Resource Identifier (URI) of the request.
content HttpContent? The HTTP content, as a list of name/value pairs, that is sent to the server.
cancellationToken CancellationToken The token to monitor for cancellation requests. Optional.

Returns

Task<HttpResponseMessage>
AnTask<HttpResponseMessage> object that represents the asynchronous operation.

Remarks

The PutAsync method sends an HTTP PUT request to the specified Uniform Resource Identifier (URI). The PUT method is used to send an entity-private entity to the web server, causing the URI identified by the RequestUri to be updated or created.

If the requestUri is not an absolute URI, it must be relative to the BaseAddress property of the HttpClient instance. If BaseAddress is null, then requestUri must be an absolute URI.

The content parameter represents the HTTP body of the request. It is of type HttpContent, which is an abstract base class for various content types like JSON, XML, or form data.

Important: You should use the CancellationToken to cancel the request if it takes too long to complete. This helps in preventing resource leaks and improving application responsiveness.

Example

Sending JSON data with PUT


using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

public class Example
{
    public static async Task UpdateResourceAsync()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://api.example.com/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Data to be updated
            var updatedData = new { Id = 1, Name = "Updated Item", Value = 100 };
            var jsonContent = new StringContent(System.Text.Json.JsonSerializer.Serialize(updatedData), Encoding.UTF8, "application/json");

            // Assuming the resource is at /items/1
            var response = await client.PutAsync("items/1", jsonContent);

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine($"Resource updated successfully. Status: {response.StatusCode}");
                // You can read the response content if needed
                // var responseContent = await response.Content.ReadAsStringAsync();
                // Console.WriteLine($"Response: {responseContent}");
            }
            else
            {
                Console.WriteLine($"Error updating resource. Status: {response.StatusCode}");
            }
        }
    }
}
            

See Also