1

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 = "[email protected]",
            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?

12
  • 3
    204 means "No Content", so there probably isn't a JSON response. Why do you think there should be? (Side note: Calling .Result is very often the wrong approach. Make use of async/await instead.) Commented Mar 22, 2019 at 18:54
  • It looks like you aren't awaiting the result of the asynchronous PUT, though I'd expect result to not have a value in that case. Is this a real API (and your real code?) and is it supposed to return a result for a PUT? Commented Mar 22, 2019 at 18:58
  • @Charleh Yes it is real code and API. Links are not real. Some properties are not real too but API is real. I have changed the names of properties/classes. How do I wait for the result of the asynchronous PUT request? Commented Mar 22, 2019 at 19:33
  • @David, How do I use aSync/Await? Commented Mar 22, 2019 at 19:33
  • 1
    @LearnAspNet: Then it sounds like the response is empty. Which a 204 HTTP code is likely to have. So what exactly is the problem? If you're expecting a response from the server but not getting one then that sounds like something to take up with whoever maintains that API. Commented Mar 22, 2019 at 20:08

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I checked the URI(REST Endpoint), nothing has been updated. Also, I am debugging my code
Please look at this answer for more detail: stackoverflow.com/a/2342589/507793
My response.Content is also empty? Is there a way I can see JSON results that PutAsJsonAsync did?
1
+50

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!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.