net\http\Request::_encode assumes the body is an array of string #602

Closed
L-P opened this Issue Jul 31, 2012 · 1 comment

Comments

Projects
None yet
2 participants
Contributor

L-P commented Jul 31, 2012

I tried this in an HTTP Adapter:

<?php
$body = array('messages' => array('foo', 'bar'));
$this->connection->post($path, $body);

This is the error I get when testing:

Exception thrown.
 File    : lithium/net/http/Request.php
 Class   : -
 Method  : join()
 Line    : 105
 ________
Array to string conversion

The request is still properly encoded and sent. A var_dump after net/http/Request.php:102 shows that _encode is called 4 times and only the first call triggers the error:

Array
(
    [type] => Array
        (
            [content] => Array
                (
                    [0] => text/html
                    [1] => application/xhtml+xml
                    [2] => */*
                )

            [options] => Array
                (
                )

        )

    [body] => Array
        (
            [messages] => Array
                (
                    [0] => foo
                    [1] => bar
                )

        )

)
Array
(
    [type] => Array
        (
            [content] => Array
                (
                    [0] => application/x-www-form-urlencoded
                    [1] => multipart/form-data
                )

            [options] => Array
                (
                    [cast] => 1
                    [encode] => http_build_query
                    [decode] => Closure Object
                        (
                            [parameter] => Array
                                (
                                    [$data] => <required>
                                )

                        )

                )

        )

    [body] => messages%5B0%5D=foo&messages%5B1%5D=bar
)
Array
(
    [type] => Array
        (
            [content] => Array
                (
                    [0] => application/x-www-form-urlencoded
                    [1] => multipart/form-data
                )

            [options] => Array
                (
                    [cast] => 1
                    [encode] => http_build_query
                    [decode] => Closure Object
                        (
                            [parameter] => Array
                                (
                                    [$data] => <required>
                                )

                        )

                )

        )

    [body] => messages%5B0%5D=foo&messages%5B1%5D=bar
)
Array
(
    [type] => Array
        (
            [content] => Array
                (
                    [0] => application/x-www-form-urlencoded
                    [1] => multipart/form-data
                )

            [options] => Array
                (
                    [cast] => 1
                    [encode] => http_build_query
                    [decode] => Closure Object
                        (
                            [parameter] => Array
                                (
                                    [$data] => <required>
                                )

                        )

                )

        )

    [body] => messages%5B0%5D=foo&messages%5B1%5D=bar
)

Here is a minimal test for net\http\Request to trigger the exception:

<?php
public function testArrayBody() {
    $this->request->body(array('mylist' => array(1, 2)));
}
Owner

nateabele commented Sep 18, 2012

Just tested and this seems to be working fine. The reason why your minimal test above fails is because you haven't set a type. If you call $this->request->type("form") or $this->request->type("json") just above the body() call, it works fine.

And in the Service class you're referencing above, the default type should always be set to "form".

@nateabele nateabele closed this Sep 18, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment