Search code examples
laravelpostdingo-api

Create object with form-encoded dot-path properties


In laravel/dingo you may create new objects by POSTing the data either as application/x-www-form-urlencoded or as application/json:

$ curl -XPOST --data name=foo http://example.org/user

JSON:

$ curl -XPOST -H 'Content-type: application/json' --data '{"name":"foo"}' http://example.org/user

Both work fine.


What I now want to do is creating objects with nested properties, e.g. name.first. This works fine when POSTing JSON:

$ curl -XPOST -H 'Content-type: application/json' --data '{"name":{"first:"foo"}}' http://example.org/user

But it fails when using form-encoded data:

$ curl -XPOST --data name.first=foo http://example.org/user

The exception is The name.first field is required.


I know that PHP converts dots to underscores:

PHP will automatically replace any dots in incoming variable names with underscores.

This might be the reason laravel does not detect the variables as being nested.


How can I make laravel detect the dot paths in variable names correctly?


Solution

  • Please use

    $ curl -XPOST --data name%5Bfirst%5D=foo http://example.org/user
    

    which is equal to name['first'] = foo

    :)