[solved] API - Can't create tickets, but GET requests work

Hello everyone,

I’m trying to set up a form in one of our web interfaces where users can post messages that get sent as new tickets to our Zammad installation.
I have created a Zammad user for this interface, that is configured with admin and agent rights and has ‘All’ rights on all existing user groups. This user has a registered API token. I can successfully access the API and get information, for example for getting a list of user groups via the /api/v1/groups endpoint.
However I can’t figure out why a POST request against the /api/v1/tickets endpoint does not work. I’m using the python requests module for convenience to do the requests, but have managed to reproduce the problem by using cUrl commands on a shell.

This works:

curl -H "Authorization: Token token=sometokenvalue; Content-Type: application/json" -X GET http://www.example.com/api/v1/groups

where the token and the hostname have been replaced with placeholder values.

This does not work:

curl -H "Authorization: Token token=sometokenvalue; Content-Type: application/json" -X POST -d '{"title":"Help me!","group": "Users","article":{"subject":"some subject","body":"some message","type":"note","internal":false},"customer":"email_of_existing_customer@example.com","note": "some note"}' http://www.example.com/api/v1/tickets

and returns a 400 reponse with the error message “article hash is missing”.

I’m starting to think that this error message is a red herring and the actual error is somewhere else, like insufficient permissions or something.

You should probably format that as code in the community, because Discourse replaced all quotes with typographical quotes, which makes it harder to reproduce your issue :confused:

I’ve replaced all typographical quotes in your example with normal quotes:

curl -H "Authorization: Token token=sometokenvalue; Content-Type: application/json" -X POST -d '{"title":"Help me!","group": "Users","article":{"subject":"some subject","body":"some message","type":"note","internal":false},"customer":"email_of_existing_customer@example.com","note": "some note"}' http://www.example.com/api/v1/tickets

With that command, I can reproduce the issue:

{"error":"article hash is missing"}

The first thing I noticed is that you mixed the two headers into one, so I separated them:

curl -H "Authorization: Token token=sometokenvalue" -H "Content-Type: application/json" -X POST -d '{"title":"Help me!","group": "Users","article":{"subject":"some subject","body":"some message","type":"note","internal":false},"customer":"email_of_existing_customer@example.com","note": "some note"}' http://www.example.com/api/v1/tickets

Then I had to replace the group (I’ve renamed my Users group) and the customer email address, and now I get a sensible response:

{
   "article_ids" : [
      46772
   ],
   "owner_id" : 1,
   "id" : 41700,
   "close_escalation_at" : null,
   "group_id" : 1,
   "first_response_diff_in_min" : null,
   "last_contact_customer_at" : null,
   "state_id" : 1,
   "test" : null,
   "priority_id" : 2,
   "ticket_time_accounting_ids" : [],
   "preferences" : {},
   "close_diff_in_min" : null,
   "close_at" : null,
   "first_response_in_min" : null,
   "customer_id" : 3259,
   "organization_id" : 2,
   "first_response_escalation_at" : null,
   "close_in_min" : null,
   "followup" : null,
   "pending_time" : null,
   "update_in_min" : null,
   "note" : "some note",
   "title" : "Help me!",
   "update_escalation_at" : null,
   "create_article_type_id" : 10,
   "time_unit" : null,
   "last_contact_at" : null,
   "last_owner_update_at" : null,
   "created_at" : "2019-03-27T10:25:42.840Z",
   "updated_at" : "2019-03-27T10:25:42.976Z",
   "updated_by_id" : 3,
   "create_article_sender_id" : 1,
   "last_contact_agent_at" : null,
   "created_by_id" : 3,
   "article_count" : 1,
   "type" : null,
   "first_response_at" : null,
   "escalation_at" : null,
   "update_diff_in_min" : null,
   "number" : "201903273800029"
}
1 Like

Hello, thanks for the reponse. I was just about to write an update on my problem.

You are right that my curl command was wrong. The headers are not supposed to be seperated by a semicolon on the same line. When I use the command with

curl -H "header1" -H "header2"

it does indeed work.

I’m still getting the same error when using the python requests module, however now that I know the curl command works, it’s save to say the problem is with my code, not the Zammad installation or anything inbetween.

Update, for the benefit of future frustrated developers stumbling over this topic:

The Zammad API requires data for POST requests, such as creating a new ticket, to be JSON-encoded in the request body. Using the python requests module, the request is done similar to this:

headers = {"Authorization":  "Token token=sometokenvalue", "Content-Type": "application/json"}
data={"key1": "value1", "key2": "value2"}
response = requests.post(url, data=data, headers=headers)

However, the data dictionary is then, by default, encoded as HTTP form-encoded parameters, like this:

key1=value1&key2=value2

The correct way is to use the json parameter of the post method (or any other):

 response = requests.post(url, json=data, headers=headers)

which results in the correct request body:

{"key1":"value1","key2":"value2"}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.