Search code examples
c#.nethttpclientcloudantjson-query

How to use Json queries with c# using HttpClient


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
      }
   }
}

Solution

  • 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>();