0

I am using C# to connect to IBM Cloudant. IBM Cloudant supports JSON queries IBM Cloudant, it is mentioned that I need to use a POST request in order to create a query but it is not explained, I am using HttpClient which has a method PostAsync method. Does anyone have an idea how to use this method to create a query for example the following query:

{
   "selector": {
      "_id": {
         "$gt": null
      }
   }
}
phuzi
  • 12,078
  • 3
  • 26
  • 50

3 Answers3

0

I had some confusion about this as well. Please refer to below.

var client = new HttpClient();

var content = new StringContent("JSON Content");
content.Headers.Add("header-name", "header value");

client.PostAsync("http://example.com/something", content);

Your JSON content can also be a C# object, which you could JSON serialize using something like Newtonsoft.Json

Madushan
  • 6,977
  • 31
  • 79
  • what should i put in the header name and the header value ? – Omar Romdhani Jul 15 '19 at 14:40
  • It was only to demonstrate how to add headers if you need to. If you don't, you can omit that line. – Madushan Jul 15 '19 at 14:42
  • The problem is that I m not getting the output, the returned HttpResponse indicates that the request was successful but I get no results within this response. `{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Transfer-Encoding: chunked X-Couch-Request-ID: fc389107cf X-Frame-Options: DENY Strict-Transport-Security: max-age=31536000 X-Content-Type-Options: nosniff X-Cloudant-Request-Class: query X-Cloudant-Backend: bm-cc-eu-gb-04 Cache-Control: must-revalidate }}` – Omar Romdhani Jul 16 '19 at 08:13
  • My code is the following: `var jsonString = "{\"selector\": {\"_id\": {\"$gt\": null}},\"fields\": [\"_id\",\"_rev\"],\"sort\": [{\"_id\": \"asc\"}]}"; var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage res = await Client.PostAsync(string.Format("https://{0}.cloudant.com/{1}/_all_docs", User, Database), content); ` – Omar Romdhani Jul 16 '19 at 08:20
  • Your request is valid (though I'm not familiar with the cloudant api you're using). You can use `res.ReadAsStringAsync().Result` to read the response body as a string (assuming you're expecting a string and there is response content) and see the server response. If this yields nothing, I suggest you consult the API documentation. (You can also use a tool like Fiddler to verify requests and responses between your app and the server. Though if you use HTTPS you need some setup for Fiddler to be able to decrypt your traffic.) – Madushan Jul 16 '19 at 08:50
  • P.S. please mark as answer if the above has solved your initial problem – Madushan Jul 16 '19 at 08:51
0

You could also try this version, classes for your query

public class Id{
    public object gt { get; set; }
}

public class Selector{
    public Id _id { get; set; }
}

public class RootObject{
    public Selector selector { get; set; }
}

serialize tmpObject and PostAsync:


client.PostAsync(url, new StringContent(tmpObject.ToString(), Encoding.UTF8, "application/json"));

mciesla
  • 72
  • 6
  • wouldn't `tmpObject.ToString()` just return the full classname ? you will need a JSON serializer or a really smart ToString override ? – Madushan Jul 15 '19 at 12:12
  • @Madushan yes, it's required to serialize it. I edited answer to be more clear – mciesla Jul 15 '19 at 12:30
0

so i finally managed to retrieve the response of the query it is as follows: var jsonString = "{\"selector\": {\"_id\": {\"$gt\": null}},\"fields\": [\"" + Attribute + "\"],\"sort\": [{\"_id\": \"asc\"}]}"; var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); HttpResponseMessage res = await Client.PostAsync(string.Format("https://{0}.cloudant.com/{1}/_find", User, Database), content); StreamReader streamReader = new StreamReader(await res.Content.ReadAsStreamAsync()); JObject responseContent = (JObject)JToken.ReadFrom(new JsonTextReader(streamReader)); streamReader.Close(); var Elements =responseContent["docs"].Value<JArray>();