0

I am trying to write simple Get operation using RestSharp and trying to use System.Test.Json to deserialize the response.

My Test method is as follows,

    [Test]
    public  void Test1()
    {
        var restClient = new RestClient("http://localhost:3333/");

        var request = new RestRequest("posts/{postid}", Method.Get);

        request.AddUrlSegment("postid", 1);

        var response = restClient.ExecuteGetAsync(request).GetAwaiter().GetResult();

        var deserial = JsonSerializer.Deserialize<Posts>(response);
    }

The Posts model class is as follows,

public class Posts
{
    public string id { get; set; }
    public string title { get; set; }
    public string author { get; set; }
}

But I am getting compellation error in line "var deserial = JsonSerializer.Deserialize(response);"

cannot convert from 'RestSharp.RestResponse' to 'System.Text.Json.JsonDocument' NUnitAPIPractice

If I changed to

var deserial = JsonSerializer.Deserialize<Posts>(response).ToSring();

Then compilation issue is fixed but then after I execute the code then I am getting

    System.Text.Json.JsonException : 'R' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
      ----> System.Text.Json.JsonReaderException : 'R' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.
  Stack Trace: 
    ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)

Please tell me do I have to use special JSON conversion to convert the RestSharp response to JSON to solve this issue ? Thanks

dbc
  • 104,963
  • 20
  • 228
  • 340
Wicky
  • 118
  • 2
  • 13
  • Try deserializing the response content: var deserial = JsonSerializer.Deserialize(response. Content). – fbede Feb 20 '22 at 07:26
  • @fbede, Thanks for the reply, Seems 'Content' does not support and only have 'response.get_Content(); So I changed to var deserial = JsonSerializer.Deserialize(response.get_Content()); But getting this System.Text.Json.JsonException : The JSON value could not be converted to System.String. Path: $.id | LineNumber: 1 | BytePositionInLine: 9. ----> System.InvalidOperationException : Cannot get the value of a token type 'Number' as a string. – Wicky Feb 20 '22 at 09:24
  • you are probably getting the exception because the type of the ID in your json is a number and not a string, try changing the type of the id property in your Posts class to int. – fbede Feb 20 '22 at 12:13
  • Thanks @fbede , it's worked after changing the Id property in Pots class to int – Wicky Feb 20 '22 at 21:27
  • I have added an answer please mark it as the correct answer if it was useful. – fbede Feb 21 '22 at 06:52

2 Answers2

3

RestSharp will deserialize it for you.

var response = await restClient.ExecuteGetAsync<Posts>(request);
var deserialized = response.Data;

Alternatively

var deserialized = await restClient.GetAsync<Posts>(request);

It's always a good idea to refer to the documentation.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
1

You are trying to deserialize the whole RESTSharp response object, not only it's json content.

The string representation of the response content is available at the Content property of your response variable, try deserializing that instead:

var deserial = JsonSerializer.Deserialize<Posts>(response.Content);

See the restsharp source code, line 56: https://github.com/restsharp/RestSharp/blob/dev/src/RestSharp/Response/RestResponseBase.cs

fbede
  • 843
  • 9
  • 12