CSharp: POST Request to Query API and JSON Parsing

Use the System.Net.WebClient object to make a POST request in C#. For parsing JSON, you can use Json.NET by James Newton-King. Install both packages using the NuGet Package Manager. This code example posts a search for the text ‘Dog’ to the Query API. This example uses the text parameter, if you want to use the file parameter to upload a document as a search parameter you will need to use multipart/form content type, with for instance RestSharp client for .NET.


string apiKey = "your-apikey";
string iodURL = "https://api.idolondemand.com/1/api/sync/query/v1";
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["apikey"] = apiKey;
formData["text"] = "Dog";
byte[] responseBytes = webClient.UploadValues(iodURL, "POST", formData);
webClient.Dispose();
UTF8Encoding enc = new UTF8Encoding();
string json = enc.GetString(responseBytes);
//Debug.WriteLine(json);

The JSON response is serialized using Json.NET into .NET Objects representing the JSON response as a DocumentList of Document type.


DocumentList documentList = JsonConvert.DeserializeObject(json);
List docs = documentList.Documents;
//
public class DocumentList
{
[JsonProperty("documents")]
public List Documents { get; set; }
}
public class Document
{
[JsonProperty("reference")]
public string Reference { get; set; }
[JsonProperty("weight")]
public double Weight { get; set; }
[JsonProperty("links")]
public List Links { get; set; }
[JsonProperty("index")]
public string Index { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("content")]
public Object Content { get; set; }
}

The above code will parse the JSON response from IDOL OnDemand.


{
"documents" : [{
"content" : { },
"index" : "news_eng",
"links" : [ "DOG" ],
"reference" : "http://feeds.theguardian.com/c/34708/f/663840/s/3605393d/sc/38/l/0L0Stheguardian0N0Cchildrens0Ebooks0Esite0C20A140Cjan0C170Creview0Etom0Ewatson0Estick0Edog0Ewants0Ea0Ehot0Edog/story01.htm",
"title" : "Stick Dog Wants a Hot Dog by Tom Watson - review",
"weight" : 89.5
},{
"content" : { },
"index" : "news_eng",
"links" : [ "DOG" ],
"reference" : "http://feeds.theguardian.com/c/34708/f/663879/s/37621ce9/sc/38/l/0L0Stheguardian0N0Ccommentisfree0C20A140Cfeb0C210Cdog0Eattacks0Eon0Echildren0Etragic0Etriggers/story01.htm",
"title" : "Dog attacks on children are tragic – but we can try to understand the triggers | Deborah Orr",
"weight" : 88.010000000000005
}
]}

Access the properties as normal:


foreach (Document doc in docs)
{
Debug.WriteLine("doc.reference: " + doc.Reference);
}

To do a multipart/form-data POST request, the simplest way is to use a client like RestSharp, which you can install via NuGet:


string apiKey = @"your-apikey";
string iodURL = @"https://api.idolondemand.com/1/api/sync/query/v1";
string file = @"C:\Users\user1\Documents\iod\TheSecretSpiritualHistoryOfCalculus_ScientificAmerican.pdf";
var client = new RestClient(iodURL);
var request = new RestRequest("", Method.POST);
request.AddParameter("apikey", apiKey);
request.AddFile("file", file);
RestResponse response = (RestResponse) client.Execute(request);
string content = response.Content;
Debug.WriteLine("status: "+ response.StatusCode + ", content: " + content);

Brian Grinstead wrote a great article on a Multipart form post in C# implementation, for those who are interested.

2 thoughts on “CSharp: POST Request to Query API and JSON Parsing

  1. Elise

    Having read this I thought it was rather enlightening.
    I appreciate you spending some time and effort to put
    this informative article together. I once again find myself personally spending a significant amount of time both reading and commenting.
    But so what, it was still worth it!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *