Creating ticket through REST API with AJAX

Hi,

I’m trying to create a ticket through the REST interface via an AJAX request. Here’s my code so far…

var fields = 'id: ticket/new\n\
Queue: Texas\n\
Requestor: requestor@rt.myrtsrv.com\n\
AdminCc: \n\
Owner: \n\
Status: new\n\
Priority: \n\
InitialPriority: \n\
FinalPriority: \n\
TimeEstimated: \n\
Starts: 2014-05-28 18:01:53\n\
Due: 2014-05-28 18:01:53\n\
Text: Yet another REST ticket';

jQuery(document).ready( function() {
    var formData = new FormData();
    formData.append( 'content', fields );

    $.ajax({
	type: 'POST',
	url: 'https://rt.myrtsrv.com/REST/1.0/edit',
	data: formData,
	processData: false,
	contentType: false,
	success:function(data){
	    $("#ajax-panel").append("<pre>"+data+"</pre>");
	}
    });
});

The server returns this respsonse:

RT/4.2.3 400 Bad Request

# Ticket new
 does not exist.

I’ve tried the same method with the rt command line tool, and it succeeds.

I can paste in the request headers and POST data this generates if it’s not obvious what I’m doing wrong.

Thanks,
Brian

 jQuery(document).ready( function() {
     var formData = new FormData();
     formData.append( 'content', fields );

     $.ajax({
 	type: 'POST',
 	url: 'https://rt.myrtsrv.com/REST/1.0/edit',

Maybe call new instead of edit?

https://rt.myrtsrv.com/REST/1.0/ticket/new

 >      jQuery(document).ready( function() {
    var formData = new FormData();
    formData.append( 'content', fields );

    $.ajax({
	type: 'POST',
	url: 'https://rt.myrtsrv.com/REST/1.0/edit',

Maybe call new instead of edit?

https://rt.myrtsrv.com/REST/1.0/ticket/new

Thanks, Jim. I should have checked the wiki. Progress, but now the server replies

RT/4.2.3 200 Ok

# Could not create ticket.
# Could not create ticket. Queue not set

The user has all ‘Modify’ rights on the queue directly, not through a group. I can tell that RT is consuming all the headers because RT logs the ‘Starts’ and ‘Due’ fields.

Any suggestions appreciated.

Thanks,
Brian

  >      jQuery(document).ready( function() {
     var formData = new FormData();
     formData.append( 'content', fields );

     $.ajax({
 	type: 'POST',
 	url: 'https://rt.myrtsrv.com/REST/1.0/edit',

Maybe call new instead of edit?

https://rt.myrtsrv.com/REST/1.0/ticket/new

Thanks, Jim. I should have checked the wiki. Progress, but now the server replies

 RT/4.2.3 200 Ok

 # Could not create ticket.
 # Could not create ticket. Queue not set

The user has all ‘Modify’ rights on the queue directly, not through a group. I can tell that RT is consuming all the headers because RT logs the ‘Starts’ and ‘Due’ fields.

Any suggestions appreciated.

Confirm the queue name in the call is exactly the same as the queue name
in RT. Also try running it with SuperUser rights. If it works you know
it’s a rights issue and can focus there (confirm SeeQueue, CreateTicket,
etc.).

Hi,

Sorry to bring up this old topic but I can’t find an answer on the internet or here.

I have the same problem as Brian_C_Duggan, when I try to create a ticket using the REST API I get an error message.

RT/4.0.13 200 Ok

# Could not create ticket.
# Could not create ticket. Queue not set

Did anyone manage to create a ticket with javascript ?

BR

Did you follow Jim’s advice? Remember that queue name case & spaces are important.

Yes of course, it’s been 3 days that I’m on this problem and I try different variants but nothing works…

Here is the request transmisted from the browser :

-----------------------------18926526828532
Content-Disposition: form-data; name="content"

id: ticket/new
Queue: SAV
Subject: Lorem Ipsum
Requestor: test@example.com
Text: dolor sit amet
-----------------------------18926526828532--

If I try a GET request to display the ticket properties, it works correctly.

What I’m doing wrong ?

If you’re up for it you could try the REST2.0 interface, personally I am much more familiar with that one as opposed to REST1.0

You are getting a session cookie to authenticate, right? And you still get this error even if the user you authenticate as is one with SuperUser rights? If so that tends to imply that there’s something amiss with passing the session cookie to the server, as a user with SuperUser rights should be able to do anything.

@knation Unfortunately for me I can’t use the REST2.0 version because it’s not implemented in our RT.

@GreenJimll The javascript code is directly loaded from the Display.html web page of RT and when I click on button that execute the Ajax call.
Anyway if the cookie was not present I would have an error like : 401 Credentials required but this is not the case.

As a proof when I make a request in GET ../REST/1.0/ticket/xxxxxx/show it works because I get a return with all the data of the ticket.

And you’ve done the same with SuperUser rights as Jim suggested?

Okay this was more work than it should have been but this works for me, I had to encode the line break using %0A:

$.ajax({
    url : "http://myrt/REST/1.0/ticket/new?user=root&pass=password&content=id: ticket/new%0AQueue: General",
    type: "GET"
});
1 Like

So now I don’t understand anything, actually with a GET method and by encoding the line break I manage to create the ticket. Your trick works perfectly !

I was trying to debug the file Ticket.pm with other method to see what args are passed and here is the dump part of result :

Args = $VAR1 = 'TimeEstimated';
$VAR2 = 0;
$VAR3 = 'Status';
$VAR4 = undef;
$VAR5 = 'Queue';
$VAR6 = 'SAV
Requestor: test@example.com
Subject: Lorem Ipsum
Text: dolor sit amet
';: ticket/new
$VAR7 = 'AdminCc';
$VAR8 = [];
$VAR9 = 'SquelchMailTo'; 

As you can see, $VAR6 take all posted data and the line break doesn’t seem to work.

Can you share the request you are using?

The initial method by @Brian_C_Duggan or yours ?

Which ever is giving you this trouble

let fields = "id: ticket/new\n Queue: SAV\n Requestor: test@example.com\n Subject: Lorem Ipsum\n Text: dolor sit amet";
let formData = new FormData();
formData.append( 'content', fields );

jQuery.ajax({
    url: '<% RT->Config->Get("WebURL") %>REST/1.0/ticket/new',
    type: 'POST',
    data: formData,
    async: false,
    success: function(data) {
        console.log(data);
    },
    cache: false,
    contentType: false,
    processData: false
});

@knation Did you manage it with POST method?

I didn’t try as the get method seemed to work