Rest API调用请求返回null。

huangapple go评论177阅读模式
英文:

Rest API call Request returning null

问题

使用 RestSharp,想要对此 API 进行简单调用:https://dbd.tricky.lol/api/characters

具体来说,我希望将其中一个角色作为对象在我的 C# 程序中使用。以下是调用:

var options = new RestClientOptions("https://dbd.tricky.lol/api");
var client = new RestClient(options);
var response = await client.GetJsonAsync<Killers>("characters?id=Chuckles");

以下是我想要反序列化的模型:

public int _id { get; set; }
public string name { get; set; }
public string gender { get; set; }
public string tunables { get; set; }
public string difficulty { get; set; }
public string height { get; set; }

响应总是为空。我做错了什么?

英文:

Using RestSharp, want to make a simple call to this API: https://dbd.tricky.lol/api/characters

Specifically, I want one of the characters as an object in my C# Program. Here's the call:

var options = new RestClientOptions(&quot;https://dbd.tricky.lol/api&quot;);
var client = new RestClient(options);
var response = await client.GetJsonAsync&lt;Killers&gt;(&quot;characters?id=Chuckles&quot;);

And Here is the Model I want to deserialize to:

public int _id { get; set; }
public string name { get; set; }
public string gender { get; set; }
public string tunables { get; set; }
public string difficulty { get; set; }
public string height { get; set; }

The response always ends up being null. What am I doing wrong?

答案1

得分: 2

你需要实际阅读API文档并调用正确的URL。另外,你的模型是错误的。以下是你的代码的正确版本:

var options = new RestClientOptions("https://dbd.tricky.lol/api");
var client = new RestClient(options);
var response = await client.GetJsonAsync<Killer>("characterinfo?character=Chuckles");

public class Killer
{
    public int index { get; set; }
    public string id { get; set; }
    public string alias {get;set;}
    public string gender { get; set; }
    public string difficulty { get; set; }
    public string height { get; set; }
    public Tunables tunables {get;set;}
}

public class Tunables
{
    public int maxwalkspeed {get;set;}
    public int terrorradius {get;set;}
    public int basebeartrapcarrycapacity {get;set;}
}

这将产生以下结果:

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/pyDx5.png

我没有添加所有的类变量,你可以自行添加,但这完成了你上面问题的请求。
英文:

You need to actually read the API doc and call the right URL. also your model is incorrect. Here is the correct version of your code

var options = new RestClientOptions(&quot;https://dbd.tricky.lol/api&quot;);
var client = new RestClient(options);
var response = await client.GetJsonAsync&lt;Killer&gt;(&quot;characterinfo?character=Chuckles&quot;);

public class Killer
{
	public int index { get; set; }
	public string id { get; set; }
	public string alias {get;set;}
	public string gender { get; set; }
	public string difficulty { get; set; }
	public string height { get; set; }
	public Tunables tunables {get;set;}
}

public class Tunables
{
	public int maxwalkspeed {get;set;}
	public int terrorradius {get;set;}
	public int basebeartrapcarrycapacity {get;set;}
}

This produces the following :

Rest API调用请求返回null。

I didn't add all the class variables for this as you can go through and do that, but this completes the request from your question above.

huangapple
  • 本文由 发表于 2023年7月5日 00:13:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定