如何使用HttpClient将带有JSON的DELETE发送到REST API

我必须使用HttpClient类向具有JSON内容的REST API服务发送删除命令,并且无法使其正常工作。

API调用:

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

因为我无法在下面的语句中添加任何内容:

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

我知道如何使用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); 

但我没有RestSharp就完成了。

虽然回答这个问题可能会迟到,但我遇到了类似的问题,以下代码对我有用。

 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); 

您可以使用以下扩展方法:

 public static class HttpClientExtensions { public static Task DeleteAsJsonAsync(this HttpClient httpClient, string requestUri, T data) => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }); public static Task DeleteAsJsonAsync(this HttpClient httpClient, string requestUri, T data, CancellationToken cancellationToken) => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }, cancellationToken); public static Task DeleteAsJsonAsync(this HttpClient httpClient, Uri requestUri, T data) => httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Delete, requestUri) { Content = Serialize(data) }); public static Task DeleteAsJsonAsync(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"); } 

Farzan Hajian的答案仍然不适合我,我可以设置请求内容,但实际上并没有发送到服务器。

作为替代方案,您可以查看使用X-HTTP-Method-Override标头。 这告诉服务器您希望它将请求视为您发送的动词与您实际发送的动词不同。 您必须确保服务器正确处理此标头,但如果确实如此,您只需POST请求并添加: X-HTTP-Method-Override:DELETE到标头,它将等同于DELETE请求身体。

试试吧

编辑

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.your.url"); request.Method = "DELETE"; request.ContentType = "application/json"; request.Accept = "application/json"; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { string json = "{\"key\":\"value\"}"; streamWriter.Write(json); streamWriter.Flush(); } using (var httpResponse = (HttpWebResponse)request.GetResponse()) { // do something with response } 

在这里你可以找到非常相似的问题。

编辑
我不确定传递DELETE请求的请求体是好方法。 特别是当这仅用于您的身份validation目的。 我更愿意将authentication_token放入Headers。 这是因为在我的解决方案中,我不必解析整个请求体来检查当前请求是否被正确认证。 其他请求类型呢? 你总是在请求体中传递authentication_token吗?