0

I'm trying to figure out why I'm getting an invalid boolean error when posting to a URL. Error and code is below.

Ideas would be greatly appreciated, I can also provide more details.

Error response:

Array( 
    [error] => Array ( 
        [errorCode] => 1008 
        [errorType] => Validation 
        [errorMessage] => Invalid boolean value 
        [obj] => collectionOnDelivery 
    ) 
    [data] => 
)

Code:

$client = new Zend_Http_Client($url);
$client->setMethod(Zend_Http_Client::POST);
$client->setHeaders('Host', 'url.api.com');    
$client->setHeaders(Zend_Http_Client::CONTENT_TYPE, 'application/json');
$client->setHeaders('Accept', 'application/json');   
$client->setHeaders('Content-Length', strlen($payload)); 
$client->setParameterPost(
    array(
        'collectionOnDelivery' => false,
        'jobId' => null,
        'invoice'  => null,
        'collectionDate' => $date,
        'consolidate' => false,
        'consignment', $payload
    ));
$response = $client->request();      

API documentation

Example Request is shown below:
POST /url HTTP/1.1
Host: url.api.com
Content-Type: application/json
Accept: application/json
Content-Length: 2416
{
"job_id": null,
"collectionOnDelivery": false,
"invoice": null,
"collectionDate": "2012-05-01T09:00:00",
"consolidate": false,
"consignment": [{ // Array of details about consignment }]
 }

Post Parameters

collectionOnDelivery boolean

consignment object[]

collectionDate date

consolidate boolean

3
  • This error seems to be coming from the API service. The answer below is probably correct but you should check the API docs to confirm the types and expected values for the collectionOnDelivery parameter. Commented Jul 31, 2014 at 23:55
  • Note that the API Doc is indicating the JSON values it expects. When you pass your array through setParameterPost, you're not sending JSON values, you're sending PHP in which will be encoded to JSON by that function (I assume). Commented Aug 1, 2014 at 16:02
  • If I use $client->setRawData($payload); Which is what I was using initially, I get response code 500, response body internal server error. Commented Aug 1, 2014 at 16:26

2 Answers 2

1

HTTP Post values are strings, so when you try to pass a boolean you are getting an error. Try replacing your booleans with strings:

'collectionOnDelivery' => "false",
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately not. So I'm guessing that the way I'm sending data is incorrect. I've updated the question showing bits of the API doc that this refers to, so any insight there would be appreciated.
You tried changing all your booleans/nulls to strings? Including that "consolidate" one and the "invoice"? What's the error you get after changing to strings?
Then I get response 500 and internal server error as the response body.
I just noticed your final item looks like incorrect syntax: 'consignment', $payload -- should that be 'consignment' => $payload ?
Try setting it to zero instead of the text "false"
0

I ended up changing to setRawData as the correct method to send the JSON data.

The reason setRawData failed the first time was because it required the consignment array to be inside another array. Translating [{ into PHP was my error in the end, which is a double array.

$content = array(
            'collectionOnDelivery' => false,
            'job_id' => null,
            'invoice'  => null,
            'collectionDate' => $date,
            'consolidate' => false,
            "consignment" => array(array(/*ARRAY OF DETAILS*/)));
$payload =  Zend_Json::encode($content);

Then setting the post as follows

$client->setRawData($payload);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.