简体   繁体   English

RESTsharp使用&获取命令

[英]RESTsharp get command with &

Hey all this is the first time I am trying out RESTsharp . 嘿,这是我第一次尝试RESTsharp I am trying to create a GET call that looks like this: 我正在尝试创建一个如下所示的GET调用:

http://labs.bible.org/api/?passage=random&type=json

I've tried the following with looking at some online examples: 我通过查看一些在线示例尝试了以下方法:

var client = new RestClient("http://labs.bible.org/");
var request = new RestRequest("api/?{0}&{1}", Method.GET);

request.AddParameter("passage", "random");
request.AddParameter("type", "json");

var queryResult = client.Execute<List<quotesAPI>>(request).Data;

When I put a stop on the queryResult it just says NULL. 当我停止queryResult时,它只会显示NULL。

quotesAPI looks like this: quotesAPI如下所示:

public class qAPI
{
   public string bookname { get; set; }
   public string chapter { get; set; }
   public string verse { get; set; }
   public string text { get; set; }
}

So how do I need to format the call in order for it to work as it should be? 因此,如何格式化呼叫以使其正常工作?

update 1 更新1

 var client = new RestClient("http://labs.bible.org/");
 var request = new RestRequest("api", Method.GET);

 request.AddParameter("passage", "random");
 request.AddParameter("type", "json");
 client.AddHandler("application/x-javascript", new RestSharp.Deserializers.JsonDeserializer());

 var queryResult = client.Execute<List<quotesAPI>>(request).Data;

First, there is no need to create rest client like this: 首先,不需要像这样创建REST客户端:

new RestRequest("api/?{0}&{1}", Method.GET);

This will result in query to http://labs.bible.org/api/?{0}&{1}&passage=random&type=json . 这将导致查询http://labs.bible.org/api/?{0}&{1}&passage=random&type=json In this specific case it might still "work", but in general you should of course avoid this. 在这种特定情况下,它可能仍然“起作用”,但是一般来说,您当然应该避免这种情况。 Instead, create it like this: 而是按以下方式创建它:

new RestRequest("api", Method.GET);

For GET methods, parameters you then create will be appended to query string for you. 对于GET方法,您随后创建的参数将被附加到查询字符串中。

Another problem here is unusual content type of response. 这里的另一个问题是响应的异常内容类型。 Response for your query has content type application/x-javascript . 您的查询响应的内容类型为application/x-javascript RestSharp has no idea what to do with such content type, so you should tell it: RestSharp不知道如何处理此类内容类型,因此您应该告诉它:

var client = new RestClient("http://labs.bible.org/");
client.AddHandler("application/x-javascript", new RestSharp.Deserializers.JsonDeserializer());

Here you say that it should deserialize response with such content type as json. 在这里,您说它应该反序列化具有json这样的内容类型的响应。 After that you should receive expected result. 之后,您应该会收到预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM