Manually debugging the SugarCRM REST API with cURL

The SugarCRM REST API is particularly difficult to debug; it’s full of bugs and inconsistencies in behaviour, and in general a malformed request simply returns an HTTP 200 code with the data string “null”. Nothing useful is posted in the application log.

To that end, I have found it essential to have cURL at my disposal to quickly push different query types into Sugar, in order to figure out which fields or parts of my API submission are causing problems.

I start by creating a couple of files containing the parameter and JSON data that I want to submit. First, we need to authenticate with the API, so we create a file like this one:

method=login
&input_type=json
&response_type=json
&rest_data={
   "user_auth":{
      "user_name":"api-test-user",
      "version":".01",
      "username":"api-test-user",
      "password":"ca63fe5287c48bac307e6c50b6f43d43"
   },
   "application_name":"Custom API"
}

This can then be submitted to the server like so:

curl -d @login.txt http://<hostname>/serivce/v4_1/rest.php

Note that in this instance, the @ in the curl parameter means that we’re specifying a file to sumbit as data. If we wanted to submit a field directly, that would be done like this:

curl -d method=login -d input_type=json -d response_type=json -d @rest-data.txt http://<hostname>/serivce/v4_1/rest.php

In order to get this format the JSON response nicely, I like to use the python JSON tool:

curl -d @login.txt http://<hostname>/dev/service/v4_1/rest.php | python -mjson.tool

This provides a nicely formatted response like this:

{
    "id": "88s7e18fbbrt5muo1mig1eov12",
    "module_name": "Users",
    "name_value_list": {
        "mobile_max_list_entries": {
            "name": "mobile_max_list_entries",
            "value": 10
        },
        "mobile_max_subpanel_entries": {
            "name": "mobile_max_subpanel_entries",
            "value": 3
        },
        "user_currency_id": {
            "name": "user_currency_id",
            "value": "-99"
        },
        "user_currency_name": {
            "name": "user_currency_name",
            "value": "British Sterling"
        },
        "user_decimal_seperator": {
            "name": "user_decimal_seperator",
            "value": "."
        },
        "user_default_dateformat": {
            "name": "user_default_dateformat",
            "value": "d.m.Y"
        },
        "user_default_team_id": {
            "name": "user_default_team_id",
            "value": "1"
        },
        "user_default_timeformat": {
            "name": "user_default_timeformat",
            "value": "h:ia"
        },
        "user_id": {
            "name": "user_id",
            "value": "4a4f2d1f-cbd4-b6b8-e3f1-102a322c6574"
        },
        "user_is_admin": {
            "name": "user_is_admin",
            "value": true
        },
        "user_language": {
            "name": "user_language",
            "value": "en_UK"
        },
        "user_name": {
            "name": "user_name",
            "value": "api-test-user"
        },
        "user_number_seperator": {
            "name": "user_number_seperator",
            "value": ","
        }
    }
}

The first part of this (the “id”) is the session ID which is required for subsequent queries.

Now you’re ready to submit general queries to the API, for example:

method=get_entry
&input_type=json
&response_type=json
&rest_data={
   "session":"88s7e18fbbrt5muo1mig1eov12",
   "module_name":"Accounts",
   "id":"76d9f1ab-312e-c673-d542-102a322c6574",
   "track_view":false
}

This will fetch the account with ID 76d9f1ab-312e-c673-d542-102a322c6574.

If you want to create an entry, you can test it like this:

method=set_entry
&input_type=json
&response_type=json
&rest_data={
   "session":"88s7e18fbbrt5muo1mig1eov12",
   "module_name":"Calls",
   "name_value_list":[
      {
         "name":"parent_type",
         "value":"Contacts"
      },
      {
         "name":"direction",
         "value":"Outbound"
      },
      {
         "name":"date_start",
         "value":"2012-11-26 19:36:02"
      },
      {
         "name":"parent_id",
         "value":"76d9f1ab-312e-c673-d542-102a322c6574"
      },
      {
         "name":"assigned_user_id",
         "value":"4a4f2d1f-cbd4-b6b8-e3f1-102a322c6574"
      },
      {
         "name":"name",
         "value":"Test call via API"
      },
      {
         "name":"description",
         "value":"This is the description"
      }
   ],
   "track_view":false
}

I hope this is helpful!

This entry was posted in APIs, Linux, PHP, SugarCRM. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What is 6 + 2 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple question (so we know that you are a human)