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
}
}
}
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
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"));
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>();
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>();
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.