无法添加参数以使用C#RestSharp客户端发出GET请求

时间:2019-10-30 08:01:36

标签: c# json restsharp

这是我第一次使用API​​,非常感谢您的帮助和耐心与我保持联系。

我正在向客户端Synccentric发出GET请求以获取数据[下面给出我正在使用的URL作为参考]。

https://api.synccentric.com/?version=latest#cb8d3255-7639-435e-9d17-c9e962c24146

[更新]

我找到了一种将参数附加到查询字符串的方法,并验证了响应。我仍然坚持传递字段数组。

            var client = new RestClient("https://v3.synccentric.com/api/v3/products");
            var request = new RestRequest(Method.GET);
            Console.WriteLine("**** Adding Headers, Content Type & Auth Key ****");
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", "Bearer {{MyAPIToken}}");
            request.AddParameter("campaign_id", 12618);
            request.AddParameter("downloadable", 1);
            request.AddParameter("downloadable_type", "csv");
            string[] fields = new[] { "asin", "upc", "actor", "all_categories", "is_eligible_for_prime", "listing_url" };
            request.AddParameter("fields", fields);
            IRestResponse response = client.Execute(request);

我想我知道问题出在哪里

Parameters List

因此,第[5]个参​​数理想情况下应保持此值“ [\ n \“ asin \”,\ n \“ upc \”,\ n \“ additional_image_1 \”,\ n \“ category \”,\ n \“ is_eligible_for_prime \”,\ n \“ listing_url \” \ n]“

但是看起来像这样。

你们能帮我吗?

我使用Python尝试了API调用并引用了文档,但确实得到了期望的响应。

在下面附加python块:

import requests
url = 'https://v3.synccentric.com/api/v3/products'
payload = "{\n    \"campaign_id\": 12618,\n    \"fields\": [\n        \"asin\",\n        \"upc\",\n        \"additional_image_1\",\n        \"category\",\n        \"is_eligible_for_prime\",\n        \"listing_url\"\n    ]\n}    #\"downloadable\":1,\n       \"downloadable_type\":\"csv\"\n}"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{MyAPIToken}}'
}
response = requests.request('GET', url, headers = headers, data = payload,  timeout= 100000 , allow_redirects= 0)
print(response.text)

执行后,我得到了想要的响应。

1 个答案:

答案 0 :(得分:1)

RestSharp不允许您发送带有内容主体的GET请求。错误说明了一切。

您必须将参数作为查询参数发送。

Console.WriteLine("**** Starting Synccentric API Fetch ****");
var client = new RestClient("https://v3.synccentric.com/api/v3/products");
var request = new RestRequest(Method.GET);

Console.WriteLine("**** Adding Headers, Content Type & Auth Key ****");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer {{MyAPIToken}}");

Console.WriteLine("**** Adding parameters ****");
request.AddParameter("campaign_id", 12618);
request.AddParameter("downloadable", "true");
request.AddParameter("downloadable_type", "CSV");

var fields = new[] { "asin", "upc", "actor", "all_categories" };
foreach (var field in fields)
{
    request.AddParameter("fields", field);
}

IRestResponse response = client.Execute(request);

这将为您构建以下查询字符串,应将其发送并希望可以理解。

https://v3.synccentric.com/api/v3/products?campaign_id=12618&downloadable=True&downloadable_type=CSV&fields=asin&fields=upc&fields=actor&fields=all_categories

更新查看了评论之后,可能是RestSharp不能与该API一起使用,因为它似乎需要带有GET请求的内容主体!