Call API using Refit and deserialize to dynamic

爱⌒轻易说出口 提交于 2020-01-04 04:01:32

问题


I'm calling a REST service using Refit and I want to deserialize the JSON that is returned as a dynamic type.

I tried defining the interface as

[Get("/foo")]
Task<dynamic> GetFoo();

but the call times out.

I know I can deserialize to a dynamic like this

var mockString = "{ title: { name: 'fred', book: 'job'} }";
dynamic d = JsonConvert.DeserializeObject(mockString);

but I can't figure out what to pass to Refit to get it to do the same.

Another option would be to get Refit to pass the raw JSON back so I can deserialize it myself but I can't see a way to do that either.

Any ideas?


回答1:


Refit uses JSON.NET under the hood, so any deserialization that works with that will work with Refit, including dynamic. The interface you have described is exactly right.

Here's a real working example:

public interface IHttpBinApi
{
    [Get("/ip")]
    Task<dynamic> GetIp();
}

var value = await RestService.For<IHttpBinApi>("http://httpbin.org").GetIp();

If you are using iOS and Refit 4+, you might be seeing this bug: https://github.com/paulcbetts/refit/issues/359

As Steven Thewissen has stated, you can use Task<string> as your return type (or Task<HttpResponseMessage>, or even Task<HttpContent>) to receive the raw response and deserialize yourself, but you shouldn't have to -- the whole point of Refit is that it's supposed to save you that hassle.




回答2:


You can define your interface to return a string and get the raw JSON that way:

[Get("/foo")]
Task<string> GetFoo();

As described here: https://github.com/paulcbetts/refit#retrieving-the-response



来源:https://stackoverflow.com/questions/46168976/call-api-using-refit-and-deserialize-to-dynamic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!