Search code examples
javascripttitaniumappceleratorcloudsight

Send post params on httpClient with square brackets


I'm trying to make an http request to a webservice called CloudSight, and I need to pass via post a series of parameters which, in explaining the documentation indicates that they should be bracketed. The curl example they offer is as follows:

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

In Titanium I have tried the following:

Var request = {
Image_request['image']: self.image,
Image_request['locale']: 'es-ES',
Image_request['language']: 'is',
};
...
Xhr.send (request);

And I get the following error: [ERROR]: Unexpected token, expected, (43:13)

I've also tried the following

Var myArray = [];
MyArray['image'] = self.image;
MyArray['locale'] = 'es-ES';
MyArray['language'] = 'es';
Var request = {
Image_request: myArray
};
...
Xhr.send (request);

In this case the application runs, but when I call the webservice I receive a 500 error. Some help? Thanks in advance.


Solution

  • If image_request [] are the parameters name then you can try something like below

    Var request = {
    "image_request[image]": self.image,
    "image_request[locale]": 'es-ES',
    "image_request[language]": 'es',
    };
    

    OR

    Var request = {
    "image": self.image,
    "locale": 'es-ES',
    "language": 'es',
    };
    

    Also confirm that server accepts json object otherwise you need to stringify it.

    Xhr.send (JSON.stringify(request));
    

    Explain more about the api parameters if it does not work.