skip to Main Content

I have the following json string http://pastebin.com/wnkiLqvP and I am trying to post it to betaface API. The post is done with PHP over cURL and the function that does the post is:

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

Also, the json string in the pastebin file is generated by the following piece of code:

$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);

where $base64 is a base64 string of a file. The base64 string was created like follows:

$file = file_get_contents($_FILES['file']['tmp_name']);
$base64 = base64_encode($file);

The API endpoint is over here http://www.betafaceapi.com/service_json.svc/UploadNewImage_File and this version of the API works with JSON. When I try to send everything to the endpoint, I should get back a response. Rathar than the response, I actually get the following after doing a cURL post with all the params:

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>

Can someone please point me out WHY do I get this error: There was an error deserializing the object of type BetafaceWebService.ImageRequestBinary. End element ‘imagefile_data’ from namespace ” expected. Found text ‘/’. ?

It seems that if I upload the file, run the POST on the API endpoint, the PHP response is NULL. But if I view source of the page, and hit refresh again, and then re-send the form submission I get the propper response from the API.

Can someone let me know what’s wrong over here? I’ve been strugling to understand this since yesterday and couldn’t find a solution.

UPDATE:
After using jsonlint with the json string PHP generates to me I get the following result:

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

Is there any limit on size for json strings?

2

Answers


  1. “/” is just escape sequence for “/” and “” is not a valid base64 character. Try remove all the “” from the base64 encoded image data.

    Login or Signup to reply.
  2. A bit late but, try adding Content-Length header

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search