Testing with command line curl can be useful since its easy to save your commands on a .txt file for future reference. The cURL webpage has more useful information including a complete scripting guide that shows how to emulate a browser with cURL: Scripting HTTP Requests Using Curl.

This example assumes you have set your services endpoint at /service and that you have enabled the comment and user resource. So lets first login to a drupal site:

curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST http://localhost:8888/service/user/login -d '
    {
        "username":"user",
        "password":"password"
    }
    '
  • `-i` means show http response headers
  • `-H` allows you to set http request headers. And drupal services need just the content-type header
  • `-c` is to save the cookies on the cookies.txt file. And since we are doing a login this is important
  • `-d` allows you to set the request body, which you will be using on drupal services to send the parameters
curl -i -H "Content-type: application/json" -b cookies.txt -X POST http://localhost:8888/service/comment -d '
    {
        "nid":"579",
        "subject":"Test Subject",
        "comment_body": { "und": [ { "value": "Test Comment" } ] }
    }
    '
  • Here we use `-b` instead of `-c` because we now want to _send_ the cookie to let drupal now that we already logged in.
curl -i -H "Content-Type: application/json" -b cookies.txt -X POST http://localhost:8888/service/user/logout

And this is just an example to logout.

If you want to see what parameters and which method (POST, GET, DELETE) to use you can open the services module files to review directly from the source ;-). This two directories are the most interesting:

  • services/resources/: contains all the resource definitions, so you can see exactly which parameters are accepted by each resource.
  • services/tests/functional/: contains functional tests, so you will see actual PHP examples of the resources being used to create, delete or retrieve data from a drupal service.

Comments

TangMonk’s picture

In 7.x-3.7
I try to use following command to create an user:

$ curl -H 'Content-type: application/json' -d '{"name":"u10", "pass": "123","mail": "user@a.om"}' http://localhost/test/api/user/register
return success:

{"uid":"11","uri":"http://drupal/daxuebao/api/user/11"}
But I try to login:

$ curl -H 'Content-type: application/json' -d '{"username":"u10", "password": "123","mail": "user@a.om"}' http://localhost/test/api/user/login
It alert me :

["Wrong username or password."]