简体   繁体   English

使用HttpClient()在C#控制台应用程序中对REST API的PUT请求

[英]PUT Request for REST API in C# console application using HttpClient()

I am very new to creating and consuming REST web services in C#. 我对使用C#创建和使用REST Web服务非常陌生。 I have programmed the data I want to send over to REST end point. 我已经对要发送到REST端点的数据进行了编程。 I am not sure if I am doing everything correctly. 我不确定我是否做得正确。 Like I mentioned previously, this is the first time I am writing a RESTful PUT request. 就像我之前提到的,这是我第一次编写RESTful PUT请求。

I have created a class and created a PUT request. 我创建了一个类并创建了一个PUT请求。 I want to see my JSON result. 我想查看我的JSON结果。 It shows no content . no content显示no content Functions are returning correct results for me. 函数为我返回正确的结果。

How can I see my JSON result? 如何查看JSON结果?

sb is object of type Student and when I am sending the request to the URL, it returns a 204 status and fails to update the record. sbStudent类型的对象,当我将请求发送到URL时,它返回204状态且无法更新记录。 I am trying to understand what is incorrect in this code: 我试图了解这段代码中的错误:

class Student
{
    public string Name{ get; set; }
    public string Email{ get; set; }
    public int Marks{ get; set; }
    public List<int> Skills{ get; set; }
}

public static void SendDataSomewhere(List<Student> studentsList)
{
    using (var client = new HttpClient())
    {
        Student sb = new Student
        {
            Name= "Test Name",
            Email = "test@gmail.com",
            Marks = HighestMarksFromList(studentsList),
            Skills = InDemandSkills(studentsList),                    
        };

        client.BaseAddress = new Uri("http://testingApi.net/");
        var response = client.PutAsJsonAsync("test", sb).Result;

        if (response.IsSuccessStatusCode)
        {
            Console.Write("Success");
        }
        else
            Console.Write("Error");
    }
}

Functions are returning correct results for me. 函数为我返回正确的结果。 var response is returning a 204 status code. var response204状态码。

How can I see my JSON result? 如何查看JSON结果?

An API returning 204 - No Content from a PUT request simply means that the API received the request and processed it, but it doesn't have anything meaningful to give back other than a successful (2xx) response status. API返回204 - No Content PUT请求中204 - No Content仅表示该API收到了该请求并对其进行了处理,但是除了成功(2xx)响应状态外,它没有任何有意义的回馈。

In a RESTful API, this is a perfectly normal response to be expecting. 在RESTful API中,这是所期望的完全正常的响应。

In order to know for sure what should be happening, you'll need to consult the documentation of the API you're using. 为了确定应该发生什么,您需要查阅所用API的文档。

You can use API testing tool like Postman or some other tools online and give the appropriate inputs in the tool. 您可以在线使用Postman等API测试工具或其他一些工具,并在工具中提供适当的输入。 Check the output for the Put query. 检查Put查询的输出。 Then you can may be know the exact problem. 然后,您可能会知道确切的问题。

In my opinion, others have already answered it via answers as well as comments. 我认为,其他人已经通过答案和评论回答了它。 But since the question is still open, let me try to answer with some more details. 但是,由于问题仍然悬而未决,让我尝试提供更多详细信息。

Your assumption that you should be able to get a JSON result as part of PUT API response is not always correct, it depends on the implementation of the API. 您认为应该能够将JSON结果作为PUT API响应的一部分的假设并不总是正确的,这取决于API的实现。

Here are different ways a PUT API can be implemented: 以下是可以实现PUT API的不同方法:

  1. PUT API giving the object in response: PUT API给出对象作为响应:

     [HttpPut] public HttpResponseMessage Put(int id, Product product) { //save the Product object. return Request.CreateResponse(HttpStatusCode.OK, product); } 

In this implementation, the API gives the object in the response, this is what you are expecting. 在此实现中,API会在响应中提供对象,这正是您所期望的。

  1. PUT API giving empty response: PUT API给出空响应:

     [HttpPut] public void Put(int id, Product product) { //save the Product object. } 

In this implementation, API returns nothing in response. 在此实现中,API不会返回任何响应。

Based on your explanation, the API you are calling is following the second way. 根据您的解释,正在调用的API遵循第二种方法。 You can verify this if you have access to the API code. 如果您有权访问API代码,则可以进行验证。

So, if your only problem is to know whether the API is not working or not, execute your code to do a PUT and then do a GET on the same object to verify if the update has been successful. 因此,如果唯一的问题是知道API是否不起作用,请执行代码以执行PUT,然后对同一对象执行GET以验证更新是否成功。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM