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 = "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?

ekad
  • 14,436
  • 26
  • 44
  • 46
Learn AspNet
  • 1,192
  • 3
  • 34
  • 74
  • 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.) – David Mar 22 '19 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? – Charleh Mar 22 '19 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? – Learn AspNet Mar 22 '19 at 19:33
  • @David, How do I use aSync/Await? – Learn AspNet Mar 22 '19 at 19:33
  • @LearnAspNet: Make the method async: `public static async Task SendDataSomewhere(List studentsList)`, await the asynchronous operation in the method: `var response = await client.PutAsJsonAsync("test", sb);`, and use `await` anywhere that you call your `SendDataSomewhere` method. – David Mar 22 '19 at 19:37
  • @David It is still returning 204. Is there a way to test it and check my JSON results? – Learn AspNet Mar 22 '19 at 19:45
  • @LearnAspNet: The `response` object should have a `Content` property which should have `ReadAs...` methods on it. You can read it as a string or as any object to which it should be able to deserialize. Don't forget to also apply `await` if using an `async` method to read the content. – David Mar 22 '19 at 19:49
  • @David, It returns empty string. – Learn AspNet Mar 22 '19 at 20:06
  • 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. – David Mar 22 '19 at 20:08
  • @David I am also concerned about the Base address. Can we chat so I can ask you few questions? – Learn AspNet Mar 22 '19 at 20:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190530/discussion-between-learn-aspnet-and-david). – Learn AspNet Mar 22 '19 at 20:49
  • What do you expect the api to return? Empty string is a valid response. Seems like you send `PUT` request to update data and you have `ok` or `not ok` response. Do you need anything else from this request? – Alexander Mar 25 '19 at 13:10

3 Answers3

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.

Matthew
  • 24,703
  • 9
  • 76
  • 110
1

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!

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.

jo_Veera
  • 293
  • 3
  • 12