[英]RestSharp - GET request with parameters
我在发送带参数的请求时遇到问题。 我有在 PHP 中发送请求的示例,但我不知道在 RestSharp 中它应该是什么样子:
如您所见,在示例中,参数被添加到私钥(我也做过),然后还有这个 CURLOPT_POSTFIELDS,其中也添加了参数。 我尝试通过 AddParameter、AddBody、AddJsonBody 来做,但没有任何效果。 当我将参数连接到私钥时,响应始终为空,如果我删除它,我会得到响应,但我的参数被忽略。
string data = "{\"Paging\":{\"per_page\":\"" + 10 + "\"}}";
RestClient client = new RestClient("api");
string header = "WMS " + publicKey + ":" + GetMd5Hash(privateKey + data);
IRestRequest request = new RestRequest("products", Method.GET);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddHeader("Authorization", header);
request.AddHeader("Content-Length", data.Length.ToString());
//request.RequestFormat = RestSharp.DataFormat.Json;
request.AddParameter("Paging", new { per_page = 10 });
IRestResponse response = client.Execute(request);
Encoding encoding = Encoding.GetEncoding("utf-8");
var result = encoding.GetString(response.RawBytes);
我能够使用 fiddler 跟踪 php 请求,原始请求如下所示:
GET api HTTP/1.1 Host: api Pragma: no-cache Accept: */* Authorization: WMS md5 Content-type: application/json Content-Length: 27 {"Paging":{"per_page":"8"}}
我的看起来像这样:
GET api HTTP/1.1 Authorization: WMS md5 Content-Type: application/json Accept: */* User-Agent: RestSharp/105.2.3.0 Host: api Accept-Encoding: gzip, deflate Connection: Keep-Alive
里面没有显示参数,不知道为什么。 我尝试了每种参数类型。 除此之外,我的标题“Content-Length”也不可见。
向 GET 请求添加参数,只需使用:
request.AddParameter("name", "value");
我认为您的代码中还有其他问题,可能是混合了 json 格式,但由于我们没有您正在使用的实际 API 的详细信息,因此我们无法提供建议。
也许您可以简化示例,对其进行测试,然后在 API 向您显示错误时添加更多配置。 在此处查看基本示例: https : //github.com/restsharp/RestSharp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.