Cloud Development Kit

Simple recognition in C

Hi,

I am trying to post a request for text recognition to MyScript cloud in c. I am using the Jansson library to parse my data into the Json format and posting it using the the CURL library. Here is the Json data that I am posting:


postfield = {
"apiKey": "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", //This is a dummy apikey, I am using my api key in my code
"hwrInput": {
 "switchToChildren": true,
 "hwrParameter": {
  "language": "en_US"
 },
 "inputUnits": [{
  "components": [{
   "type": "stroke",
   "X": [438.0, 439.0, 439.0],
   "Y": [319.5, 313.5, 312.5]
  }]}]}
}

And here is the code I am using to post using CURL:
The URL is declared in main as:

char *url = "https://cloud.myscript.com/api/myscript/v2.0/hwr/doSimpleRecognition.json";



static char *request(const char *url, char *postfield){
CURL *curl = NULL;
 CURLcode status;
 struct curl_slist *headers = NULL;
 char *data = NULL; //data that will be returned by the request
 long code;
 curl_global_init(CURL_GLOBAL_ALL);
 curl = curl_easy_init();
 if(!curl)
goto error;

data = malloc(BUFFER_SIZE);
if(!data)
goto error;

struct write_result write_result = {
.data = data,
.pos = 0
};
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfield);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
status = curl_easy_perform(curl);

if(status != 0){
fprintf(stderr, "error: unable to request data from %s:n", url);
fprintf(stderr, "%sn", curl_easy_strerror(status));
goto error;
}

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200){
fprintf(stderr, "error: server responded with code %ldn", code);
goto error;
}

curl_easy_cleanup(curl);
curl_global_cleanup();

data[write_result.pos] = '';

return data;

error:
  if(data)
   free(data);
  if(curl)
   curl_easy_cleanup(curl);
  if(headers)
   curl_slist_free_all(headers);
   curl_global_cleanup();
  return NULL;
}


Unfortunately, every time I send a post the cloud, I get 400 error code implying that I have a bad request. I was wondering whether there is something I am doing wrong. Also, do you have an example code of doing a simple recognition in C.

Thanks


An update:

The exact message I am getting is: {result:{message:Required String parameter 'apiKey' is not present}}

I don't understand why this is the case given I am actually sending an application key that is active and has HMAC disabled.

Dear Sir,

can you please try using another curl library, probably the one you are using is not formatting the message properly.

Best regards,

Olivier

Login or Signup to post a comment