我发送带参数的请求时遇到问题。我有用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"也不可见。
答案 0 :(得分:0)
将参数添加到GET请求,只需使用:
request.AddParameter("name", "value");
我认为您的代码中存在其他问题,可能是混合了json格式,但由于我们没有您正在使用的实际API的详细信息,我们可以提供建议。
可能是您可以简化示例,测试它,然后添加更多配置,因为API会显示错误。请查看此处的基本示例:https://github.com/restsharp/RestSharp