[英]Not able to add parameters to make GET request with C# RestSharp client
This is my first time working with APIs and I'd really appreciate your help and patience on bearing with me.这是我第一次使用 API,非常感谢您对我的帮助和耐心。
I'm making a GET request to the client Synccentric for getting data [Given URL below I'm using for ref].我正在向客户端 Synccentric 发出 GET 请求以获取数据 [给定下面的 URL 我用于参考]。
https://api.synccentric.com/?version=latest#cb8d3255-7639-435e-9d17-c9e962c24146 https://api.synccentric.com/?version=latest#cb8d3255-7639-435e-9d17-c9e962c24146
[Update] [更新]
I found a way to attach parameters to querystrings and the response was validated.我找到了一种将参数附加到查询字符串的方法,并且响应得到了验证。 I'm still stuck with passing the array of fields.
我仍然坚持传递字段数组。
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);
I think I know where the problem is我想我知道问题出在哪里
So the [5]th parameter should ideally hold this value "[\n \"asin\",\n \"upc\",\n \"additional_image_1\",\n \"category\",\n \"is_eligible_for_prime\",\n \"listing_url\"\n ]"所以第[5]个参数理想情况下应该保持这个值 "[\n \"asin\",\n \"upc\",\n \"additional_image_1\",\n \"category\",\n \" is_eligible_for_prime\",\n \"listing_url\"\n ]"
But instead it looks like this.但相反,它看起来像这样。
Can you guys help me with this?你们能帮我解决这个问题吗?
I tried the API call using Python and referencing the documents and I did get the desired response.我尝试使用 Python 调用 API 并参考文档,我确实得到了所需的响应。
Attaching the python block below:在下面附加 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)
After the execution I got the response I was looking for.执行后,我得到了我正在寻找的回应。
RestSharp will not allow you to send a GET request with a content-body. RestSharp 不允许您发送带有内容主体的 GET 请求。 The error says it all.
错误说明了一切。
You will have to send the parameters as query parameters.您必须将参数作为查询参数发送。
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);
This will build you the following query string, which should be sent and hopefully understood okay.这将为您构建以下查询字符串,应该发送并希望可以理解。
https://v3.synccentric.com/api/v3/products?campaign_id=12618&downloadable=True&downloadable_type=CSV&fields=asin&fields=upc&fields=actor&fields=all_categories
UPDATE Having looked at the comments, it may be that RestSharp cannot be used with that API as it seems that it requires content body with a GET request!更新查看评论后,可能是 RestSharp 不能与 API 一起使用,因为它似乎需要带有 GET 请求的内容正文!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.