简体   繁体   中英

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

I am very new to creating and consuming REST web services in C#. I have programmed the data I want to send over to REST end point. 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.

I have created a class and created a PUT request. I want to see my JSON result. It shows no content . Functions are returning correct results for me.

How can I see my JSON result?

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. 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.

How can I see my JSON result?

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.

In a RESTful API, this is a perfectly normal response to be expecting.

In order to know for sure what should be happening, you'll need to consult the documentation of the API you're using.

You can use API testing tool like Postman or some other tools online and give the appropriate inputs in the tool. Check the output for the Put query. 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.

Here are different ways a PUT API can be implemented:

  1. PUT API giving the object in response:

     [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.

  1. PUT API giving empty response:

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

In this implementation, API returns nothing in response.

Based on your explanation, the API you are calling is following the second way. You can verify this if you have access to the API code.

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.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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