I need to do a PATCH
request with the Windows.Web.Http.HttpClient
class and there is no official documentation on how to do it. How can I do this?<
I'd like to extend on @alexander-pacha's answer and suggest adding following extension class somewhere in a common library. Wether this be a common library for a project / client / framework/... is something you'll have to make out on your own.
public static class HttpClientExtensions
{
///
/// Send a PATCH request to the specified Uri as an asynchronous operation.
///
///
///
/// Returns .The task object representing the asynchronous operation.
///
/// The instantiated Http Client
/// The Uri the request is sent to.
/// The HTTP request content sent to the server.
/// The was null.
/// The was null.
public static Task PatchAsync(this HttpClient client, string requestUri, HttpContent content)
{
return client.PatchAsync(CreateUri(requestUri), content);
}
///
/// Send a PATCH request to the specified Uri as an asynchronous operation.
///
///
///
/// Returns .The task object representing the asynchronous operation.
///
/// The instantiated Http Client
/// The Uri the request is sent to.
/// The HTTP request content sent to the server.
/// The was null.
/// The was null.
public static Task PatchAsync(this HttpClient client, Uri requestUri, HttpContent content)
{
return client.PatchAsync(requestUri, content, CancellationToken.None);
}
///
/// Send a PATCH request with a cancellation token as an asynchronous operation.
///
///
///
/// Returns .The task object representing the asynchronous operation.
///
/// The instantiated Http Client
/// The Uri the request is sent to.
/// The HTTP request content sent to the server.
/// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
/// The was null.
/// The was null.
public static Task PatchAsync(this HttpClient client, string requestUri, HttpContent content, CancellationToken cancellationToken)
{
return client.PatchAsync(CreateUri(requestUri), content, cancellationToken);
}
///
/// Send a PATCH request with a cancellation token as an asynchronous operation.
///
///
///
/// Returns .The task object representing the asynchronous operation.
///
/// The instantiated Http Client
/// The Uri the request is sent to.
/// The HTTP request content sent to the server.
/// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
/// The was null.
/// The was null.
public static Task PatchAsync(this HttpClient client, Uri requestUri, HttpContent content, CancellationToken cancellationToken)
{
return client.SendAsync(new HttpRequestMessage(new HttpMethod("PATCH"), requestUri)
{
Content = content
}, cancellationToken);
}
private static Uri CreateUri(string uri)
{
return string.IsNullOrEmpty(uri) ? null : new Uri(uri, UriKind.RelativeOrAbsolute);
}
}
This way you're not awaiting and holding up execution in some static extension class, but you handle that as if you were really doing a PostAsync
or a PutAsync
call. You also have the same overloads at your disposal and you're letting the HttpClient
handle everything it was designed to handle.