【问题标题】:Error sending request to API with a json string使用 json 字符串向 API 发送请求时出错
【发布时间】:2014-02-12 04:28:57
【问题描述】:

我有以下 json 字符串 http://pastebin.com/wnkiLqvP,我正在尝试将其发布到 betaface API。该帖子是通过 cURL 使用 PHP 完成的,执行该帖子的函数是:

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 端点位于此处http://www.betafaceapi.com/service_json.svc/UploadNewImage_File,此版本的 API 可使用 JSON。当我尝试将所有内容发送到端点时,我应该得到响应。与响应相比,在使用所有参数进行 cURL 发布后,我实际上得到了以下信息:

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>

谁能指出我为什么会收到此错误:反序列化 BetafaceWebService.ImageRequestBinary 类型的对象时出错。应来自命名空间“”的结束元素“imagefile_data”。找到文本“/”。 ?

似乎如果我上传文件,在 API 端点上运行 POST,PHP 响应为 NULL。但是,如果我查看页面的源代码,然后再次点击刷新,然后重新发送表单提交,我会从 API 获得适当的响应。

谁能告诉我这里出了什么问题?从昨天开始我一直在努力理解这一点,但找不到解决方案。

更新: 将 jsonlint 与 PHP 生成给我的 json 字符串一起使用后,我得到以下结果:

Parse error on line 4:
...  "imagefile_data": "\/9j\/4AAQSkZJRgABA
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

json 字符串的大小有限制吗?

【问题讨论】:

  • 你应该隐藏你的 api 秘密。
  • 这是一个公共秘密 :) 这不是问题。但是谢谢你的意图

标签: javascript php json api curl


【解决方案1】:

有点晚了,尝试添加 Content-Length 标头

【讨论】:

    【解决方案2】:

    "/" 只是 "/" 的转义序列,而 "\" 不是有效的 base64 字符。尝试从 base64 编码的图像数据中删除所有“\”。

    【讨论】:

    • 删除所有“\”字符后尝试发送帖子时出现 500 内部服务器错误
    • 我没有收到任何错误说明。 API 端点返回“500 INTERNAL SERVER ERROR”。
    猜你喜欢
    • 2018-06-02
    • 2020-07-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    相关资源
    最近更新 更多