Javascript 使用json字符串向API发送请求时出错
我有以下json字符串,我正试图将其发布到BetaFaceAPI。post是用PHP over cURL完成的,完成post的函数是:Javascript 使用json字符串向API发送请求时出错,javascript,php,json,api,curl,Javascript,Php,Json,Api,Curl,我有以下json字符串,我正试图将其发布到BetaFaceAPI。post是用PHP over cURL完成的,完成post的函数是: function bcall_endpoint($endpoint,$json_params){ //Init CURL $ch = curl_init($endpoint); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER,
function bcall_endpoint($endpoint,$json_params){
//Init CURL
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
return $result;
}
此外,pastebin文件中的json字符串由以下代码段生成:
$beta = array(
'api_key'=>'d45fd466-51e2-4701-8da8-04351c872236',
'api_secret'=>'171e8465-f548-401d-b63b-caf0dc28df5f',
'imagefile_data'=>$base64,
'detection_flags'=>'cropface,recognition,propoints,classifiers,extended'
);
$beta = json_encode($beta);
其中$base64是文件的base64字符串。base64字符串的创建如下所示:
$file = file_get_contents($_FILES['file']['tmp_name']);
$base64 = base64_encode($file);
API端点在这里,此版本的API与JSON一起工作。当我尝试将所有内容发送到端点时,应该会得到响应。与响应相比,在使用所有参数执行cURL post之后,我实际上得到了以下结果:
HEADERS
Access-Control-Allow-Origin: *
Cache-Control: private
Content-Length: 1110
Content-Type: text/html
Date: Wed, 22 Jan 2014 07:55:32 GMT
Server: Microsoft-IIS/7.5
X-Aspnet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
BODY view raw
<html version="-//W3C//DTD XHTML 2.0//EN" xml:lang="en" xsi:schemaLocation="http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<HEAD>
<TITLE>Request Error</TITLE>
</HEAD>
<BODY>
<DIV id="content">
<P class="heading1">
<B>Error Status Code:</B> 'InternalServerError'
</P>
<P>
<B>Details: </B>There was an error deserializing the object of type BetafaceWebService.ImageRequestBinary. End element 'imagefile_data' from namespace '' expected. Found text '/'.
</P>
<!-- Padding xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
</DIV>
</BODY>
</html>
json字符串的大小有限制吗?“/”只是“/”的转义序列,“\”不是有效的base64字符。尝试从base64编码的图像数据中删除所有“\”。有点晚,但是,尝试添加内容长度标题您应该隐藏api机密。这是一个公共机密:)这不是问题所在。但是谢谢你的意图,当我删除所有“\”字符后试图发送帖子时,我收到了500个内部服务器错误。我没有收到任何错误规范。API端点返回“500内部服务器错误”。
Parse error on line 4:
... "imagefile_data": "\/9j\/4AAQSkZJRgABA
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['