简体   繁体   中英

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.

I'm making a GET request to the client Synccentric for getting data [Given URL below I'm using for ref].

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 ]"

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.

Attaching the python block below:

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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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