2017-07-12 49 views
0

我想向一个名为CloudSight的web服务发出一个http请求,并且我需要通过post传递一系列参数,在解释文档时指示它们应该被括起来。 他们提供的卷曲示例如下:用方括号在httpClient上发送params

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 

钛我曾尝试以下:

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

而且我得到以下错误: [错误]:意外的令牌,预计,( 43:13)

我也试过以下

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

在这种情况下应用程序运行,但是当我调用web服务时,我收到500错误。 有些帮助? 在此先感谢。

回答

1

如果image_request []是参数的名称,那么你可以尝试像下面

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', 
}; 

还请确认服务器接受JSON对象,否则你需要字符串化它。

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

解释有关api参数的更多信息,如果它不起作用。

+0

非常感谢。我已经将参数的名称放在引号中,正如您建议的那样,它的工作完美无缺。 –