asp.net 使用RestSharp删除函数IRestResponse

ezykj2lf  于 2023-02-01  发布在  .NET
关注(0)|答案(2)|浏览(105)

我想用RestSharp的Restful API实现一个删除函数。我已经实现了GET和POST函数。但是使用这个删除函数,我没有从API那里得到任何反馈,只有一个http响应。我的问题是我如何编写代码,以便我可以执行删除函数?我已经有了这个:

//    var url = string.Format("url");

//    Construct the request.
//    var request = new RestRequest(url, Method.DELETE);

//    //IRestResponse<> response;
//    //response = await restClient.ExecuteTaskAsync<>(request);
//    //return response.Data;
6yjfywim

6yjfywim1#

试试看

var client = new RestClient(url);
var request = new RestRequest(Method.DELETE);
IRestResponse response = client.Execute(request);
erhoui1w

erhoui1w2#

这里的重点是在标题中包含“Accept”,并在代码中显示值。

string url = "http://localhost:8080/laptop-bag/webapi/api/delete/2";

        IRestRequest restRequest = new RestRequest(url);
        restRequest.AddHeader("Accept", "*/*"); //Important

        IRestClient restClient = new RestClient();
        IRestResponse restResponse = restClient.Delete(restRequest);
        
        //Status code will be 200 for successful execution
        int statusCode = Convert.ToInt32(restResponse.StatusCode);

相关问题