1

I'm trying to create an OTRS ticket.

Web service type HTTP:REST.

var data = {
  Ticket: {
    Title: "123123",
    TypeID: "2",
    QueueID: "1",
    State: "open",
    PriorityID: "2",
    ServiceID: "1"
  },
  Article: {
    Subject: "123123",
    Body: "Trololo",
    ContentType: "text/plain; charset=utf8"
  },
  SessionID: 123
};

$.ajax({
  url: url,
  type: 'POST',
  dataType: 'json',
  data: JSON.stringify(data),
  success: function(res) {
    ...
  },
  error: function(res) {
    ...
  }
});

And after submitting I got an error: Could not read input data.

If I try change method to GET and change the data type to JS obj I've got another

error:
ErrorCode:"TicketCreate.MissingParameter"
ErrorMessage:"TicketCreate: Ticket parameter is missing in or not valid!"

What am I doing wrong?

Andrew Bone
  • 7,092
  • 2
  • 18
  • 33
Alex
  • 11
  • 3

3 Answers3

0

It is because of CORS; which basically means you're not allowed to send POST requests to any URL from your browser. If you want to be allowed to do that, you must change your web server configuration.

You'll most probably want to send the request to OTRS REST API from the server side, by the way, and not from the client side. If you'd send it from the client side you need to store the access token or similar on the client as well, which in most cases would be a Bad Idea. A nice side effect from this is that it would fix your CORS issue as well.

For explanation of CORS check this:

How does Access-Control-Allow-Origin header work?

And for specific explanation on why in some scenarios plain JS would work where jQuery does not:

A CORS POST request works from plain javascript, but why not with jQuery?

MichielB
  • 4,181
  • 1
  • 30
  • 39
0

You payload seems to be to easy. :-D The REST interface of OTRS is limited and not so easy to use. For example if you want to create an ticket via curl the payload need to look similar like that:

curl -X POST -H "Content-Type: application/json" --data '{"UserLogin" : "ZnunyAgent","Password" : "znuny4otrs","Ticket" : {"Title" : "some ticket title","Queue" : "Postmaster","State" : "new","PriorityID" : "3","CustomerUser" : "znuny@znuny.com"},"Article" : {"Subject" : "some subject","Body" : "some body","ContentType" : "text/plain; charset=ISO-8859-15","MimeType" : "text/plain", "Charset" : "ISO-8859-15"}}' http://localhost/otrs/nph-genericinterface.pl/Webservice/1/TicketCreate

As you can see, you need to take care/adopt the following to your Ajax request:

1) Put user for authentication into the payload (or create a user session first)

2) Use the correct url with the configured webserice id in the url

Rob
  • 26,989
  • 16
  • 82
  • 98
0

The following worked for me.

I also added a Chrome extension for CORS to work around the localhost CORS restrictions.

var data =
{
    "Ticket": {
        "Title": "Rest Create",
        "Type": "Support",
        "Queue": "{some queue here}",
        "State": "New",
        "Priority": "3 Important",
        "CustomerUser": "{some email here}"
    },
        "Article": {
        "Subject": "Rest Create",
        "Body": "Test",
        "ContentType": "text/plain; charset=utf8"
    }
};


$.ajax({
    url: "http://dev-otrs-1/otrs/nph-genericinterface.pl/Webservice/{CONNECTORNAME}/Ticket?UserLogin={USERNAME}&Password={PASSWORD}",
    type: "post",
    data: JSON.stringify(data),
    success: function (response) {
        console.debug(response);
    }
});