0

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

Parameters List

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.

  • 1
    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 e.g. `request.AddParameter("campaign_id", 12618);` – phuzi Oct 30 '19 at 08:11
  • 1
    GET doesnt have a body parameter, only post. As phuzi says all get parameters are in the url line – BugFinder Oct 30 '19 at 08:34
  • @phuzi Thanks for your response, How do i pass the array of fields as string now? – Shreyas Sawant Oct 30 '19 at 08:46
  • From the [docs](https://api.synccentric.com/?version=latest#cb8d3255-7639-435e-9d17-c9e962c24146), unless I've read it wrong, that API appears to violate normal HTTP rules/conventions by expecting a request body in a GET request. That's very unusual and not normally expected. You might have to use a different HTTP client library which is more relaxed about what you put in your requests. – ADyson Oct 30 '19 at 09:24
  • 1
    Elasticsearch do the same thing. ( https://www.elastic.co/guide/en/elasticsearch/guide/current/_empty_search.html ) but at least it provides the same operation with POST just in case... Anyway, the fault is the library not the API. Nothing in the RFCs 7230-7237 forbide body data in GET (https://stackoverflow.com/questions/978061/http-get-with-request-body) and in any case it is server work to ignore de body or respond with a http code that indicates the problem. if I were you the first thing I would try is just change Method.GET to POST; just in case it works... – jlvaquero Oct 30 '19 at 09:34

1 Answers1

1

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!

phuzi
  • 8,111
  • 3
  • 24
  • 43
  • @phuzi Thank you for this approach, Your answer gave me a clarity on how the QueryString was expecting fields. You were absolutely correct with the foreach clause. Thanks Again !! – Shreyas Sawant Nov 19 '19 at 10:46