Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send DELETE with JSON to the REST API using HttpClient

I have to send a delete command to a REST API service with JSON content using the HttpClient class and can't make this working.

API call:

DELETE /xxx/current {  "authentication_token": "" } 

because I can't add any content into below statement:

HttpResponseMessage response = client.DeleteAsync(requestUri).Result; 

I know how to make this work with RestSharp:

var request = new RestRequest {     Resource = "/xxx/current",     Method = Method.DELETE,     RequestFormat = DataFormat.Json };  var jsonPayload = JsonConvert.SerializeObject(cancelDto, Formatting.Indented);  request.Parameters.Clear(); request.AddHeader("Content-type", "application/json"); request.AddHeader ("Accept", "application/json"); request.AddParameter ("application/json", jsonPayload, ParameterType.RequestBody);  var response = await client.ExecuteTaskAsync (request); 

but I have get it done without RestSharp.

like image 330
Tomasz Kowalczyk Avatar asked Jan 20 '15 20:01

Tomasz Kowalczyk


People also ask

How do I write a delete method in REST API?

In REST API DELETE is a method level annotation, this annotation indicates that the following method will respond to the HTTP DELETE request only. It is used to delete a resource identified by requested URI. DELETE operation is idempotent which means. If you DELETE a resource then it is removed, gone forever.

What is the return type of HttpClient post delete request?

HttpClient is one of the best APIs to make HTTP requests. It returns an Observable, but if you want to return a Promise that can also be done using HttpClient. It is best practice to return an Observable and subscribe it in other functions.


2 Answers

Although it might be late to answer this question but I've faced a similar problem and the following code worked for me.

HttpRequestMessage request = new HttpRequestMessage {     Content = new StringContent("[YOUR JSON GOES HERE]", Encoding.UTF8, "application/json"),     Method = HttpMethod.Delete,     RequestUri = new Uri("[YOUR URL GOES HERE]") }; await httpClient.SendAsync(request); 

UPDATE on .NET 5

.NET 5 introduced JsonContent. Here is an extension method using JsonContent:

public static async Task<HttpResponseMessage> DeleteAsJsonAsync<TValue>(this HttpClient httpClient, string requestUri, TValue value) {     HttpRequestMessage request = new HttpRequestMessage     {         Content = JsonContent.Create(value),         Method = HttpMethod.Delete,         RequestUri = new Uri(requestUri, UriKind.Relative)     };     return await httpClient.SendAsync(request); } 
like image 143
Farzan Hajian Avatar answered Sep 22 '22 23:09

Farzan Hajian


You can use these extension methods:

public static class HttpClientExtensions {     public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, string requestUri, T data)         => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) });      public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, string requestUri, T data, CancellationToken cancellationToken)         => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }, cancellationToken);      public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, Uri requestUri, T data)         => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) });      public static Task<HttpResponseMessage> DeleteAsJsonAsync<T>(this HttpClient httpClient, Uri requestUri, T data, CancellationToken cancellationToken)         => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }, cancellationToken);      private static HttpContent Serialize(object data) => new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); } 
like image 27
huysentruitw Avatar answered Sep 21 '22 23:09

huysentruitw