0

I'm using this API that accepts an image and sends back the description of it's contents. According to the docs, the cURL looks like this:

 curl -i -X POST \
-H "Authorization: CloudSight [key]" \
-F "image_request[image]=@Image.jpg" \
-F "image_request[locale]=en-US" \
https://api.cloudsightapi.com/image_requests

My code for sending the request is as below:

var request = (HttpWebRequest)WebRequest.Create(cloudsight_url);
request.Method = "POST";
request.Headers.Add("Authorization", "CloudSight R6USrcKxMym0EP2peuYtVA");
string s1 = string.Format("image_request[locale]={0}&image_request[remote_image_url]={1}", "en-US", imgurl);

// Send Data
StreamWriter myWriter = null;
myWriter = new StreamWriter(request.GetRequestStream());
myWriter.Write(s1);
myWriter.Close();
var response1 = (HttpWebResponse)request.GetResponse();
string result2 = "";

using (StreamReader streamReader = new StreamReader(response1.GetResponseStream()))
{
    var output = streamReader.ReadToEnd();
    streamReader.Close();
    result2 = output.ToString();    
}

The request is sent successfully, however the service returns { "status" : "not completed" }. In the docs, it says to continue polling for a response until the { "status" : "completed" } response is returned. How do I acheive this?

CDub
  • 13,146
  • 4
  • 51
  • 68
psyoptica
  • 57
  • 1
  • 9

1 Answers1

1

As you pointed out the documentation states that you should continue polling until the response has been marked completed.

All you really need to do is to put your HTTP web request into a loop that continues until completion. Probably also a good idea to exit the loop after a certain time without success to avoid looping infinitely. Putting the loop onto a separate thread might also be wise.

MeterLongCat
  • 228
  • 2
  • 11
  • Which part of the http web request will I be putting inside the loop? Wouldn't it send the same request over and over? – psyoptica Nov 12 '16 at 08:24
  • According tot he documentation you obtain a token from the `/image_requests`. You then supply this token to the retrieval of the image response. – MeterLongCat Nov 12 '16 at 08:27
  • yes but the token doesn't work for an incomplete request. Or do you mean to say that I need to use this token to send another request and so on? – psyoptica Nov 12 '16 at 08:30
  • To clarify further, you should be receiving a token as a result of your `/image_requests` call. Use that token to then call `/image_responses/[token]`. – MeterLongCat Nov 12 '16 at 08:31
  • I get a token in response but the request status is incomplete, Using this token doesn't work. – psyoptica Nov 12 '16 at 08:33
  • Can u please explain how I can put the request in a loop? – psyoptica Nov 12 '16 at 08:34
  • Perhaps I'm not being clear enough. It should look something like this (pseudocode): – MeterLongCat Nov 12 '16 at 08:34
  • result = HttpRequest("/image_requests"); while (!complete) { HttpRequest("/image_responses/result.token") } – MeterLongCat Nov 12 '16 at 08:36
  • ohh okay gotcha..so basically I will be sending the same request with the token from the previous response until I get the response status as completed – psyoptica Nov 12 '16 at 08:38