简体   繁体   English

使用参数使用 restSharp 获取 HTTP 请求

[英]HTTP Get request with restSharp using Parameters

I did this request in postman and it works fine我在邮递员那里做了这个请求,它工作正常

在此处输入图片说明

But when I try to do the same request using RestSharp I´m having this response:但是当我尝试使用 RestSharp 执行相同的请求时,我得到了以下响应:

在此处输入图片说明

my csharp code:我的 csharp 代码:

        public string FindWeatherStation(string query)
        {
            RestClient client = new RestClient(new Uri($"https://api.meteostat.net/v2/stations/search"));
            var request = new RestRequest(Method.GET);

            request.AddHeader("x-api-key", "mykey");
            request.AddParameter("query", query);
            IRestResponse response = client.Execute(request);
            var result = response.Content;

            return result;

        }

the query value is vancouver as well查询值也是温哥华

What am I doing wrong?我究竟做错了什么?

Creating the RestClient object by simply pass the url as string:通过简单地将 url 作为字符串传递来创建 RestClient 对象:

RestClient client = new RestClient("https://api.meteostat.net/v2/stations/search");

Then using ParameterType.RequestBody as the third parameter to the AddParameter method:然后使用ParameterType.RequestBody作为AddParameter方法的第三个参数:

request.AddParameter("query", query, ParameterType.RequestBody);

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

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