Posting CustomFields with REST2


#1

Hello i cant seem to find in the documentation or by trial/error how to post custom field values using the /REST/2.0/ticket endpoint.

I can successfully post and create new tickets no problem, just cant add custom fields and i don’t get an error response either.

What is the JSON format expected to include a custom field in my payload?

PS. i have tried the REST v1 format of {"CF-MyCustomFieldName": "value"} and this does not work.

i have also tried {"CF.MyCustomFieldName": "value"} , {"CF.ID": "value"}


#2

{“CustomFields”: {“MyCustomFieldName”: “Hello world”} }

Should work for you, where within CustomFields you can have an array of hashes with the CustomField id/Name and the desired value.


#4

I’d REALLY like to see a more complete example that includes a) Two or more non-custom fields and b) Two or more custom fields, with the exact JSON syntax that your system requires. There are usually some fields of each type in a request to create a ticket. I’ve created valid multi-dimensional arrays and used json_encode(array) to produce the JSON to send to the API. However, it consistently fails to set any of the Custom Fields. I had far better success entering all the fields as if they were non-custom. Only one Custom field failed to be set in the ticket - and it had been created (in the admin UI) exactly the same way as other Custom fields were created.


#5

Create a ticket, setting some custom fields

curl -X POST -H "Content-Type: application/json" -u 'root:password'
    -d '{ "Queue": "General", "Subject": "Create ticket test",
        "From": "user1@example.com", "To": "rt@example.com",
        "Content": "Testing a create",
        "CustomFields": {"Field1": "Value1"}, {"Field2" : "value2"}}'
    'https://myrt.com/REST/2.0/ticket'

Does this example give you an idea of how to set your CF and non CF fields?


#6

Thanks, Craig.

I may have tried that format but I’ll try it again and let you know.

Steve


#7

Nope. completely fails to create a ticket. Perhaps some of the other code RT gave us (which sends the request to /rest/api and not REST/2.0/ticket) is at fault here? Here is that code (php script fragment):

$service_url = “https://myrt/rest/api”;
$params = [ ‘data’ => $jsonstr ];
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($ch);


#8

Looking at the code it looks like you need to use the customfield ID value, so something like this:

curl -X POST -H "Content-Type: application/json" -u 'root:password' -d '{ "Queue": "Incidents", "Subject": "Create ticket test", "Content": "Testing a create", "Owner": "root", "CustomFields": {"10": "1.2.3.4", "17": "Some other customfield value"}}' 'http://localhost:8080/REST/2.0/ticket'

#9

Hi Craig,

I’ve made that change so that the JSON string looks like this:
$jsonstr = ‘{“requestor”: "’ . $requestor . ‘", “subject”: "’ . $subject . ‘", “content”: "’ . $content . ‘", “queue”: "’ . $queue . ‘", “CustomFields”: {“5”: "’ . $name . ‘", “4”: "’ . $phone . ‘", “6”: "’ . $email . ‘", “14”: "’ . $account_number . ‘", “13”: "’ . $service_address . ‘", “9”: "’ . $service_type . ‘", “8”: "’ . $service_provider . ‘", “19”: "’. $ticket_type . ‘"}}’;

{It remains unclear whether or not the JSON string needs to be encoded. I’ve had no success either way. This suggests that while one of those is problematic, the root cause of failure lies elsewhere. }

Still cannot create the ticket. Could there be a problem with authorization? We have a username, a password, and a token hash that was created from username & password.

What specific authorization format does this REST 2.0 API expect (in the context of a PHP CURL script; nothing that would otherwise e entered at a command line is of any use to us because we must automate this process via a web page containing a form and a PHP script called by that form)?

Would either of these be expected to work?
curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Authorization: token $token”,‘Content-Type: application/json’));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’));
curl_setopt($curl, CURLOPT_USERPWD, “username:password”);

Thanks in advance