Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

Create a ticket with API and ajax

Hello,

im trying to create a ticket with the osticket API. With the cURL in php its working fine

With ajax i have always this error: Failed to load resource: the server responded with a status of 401 (Unauthorized)

This is my request headers

POST /~xxx/ticket19/api/http.php/tickets.json HTTP/1.1
Host: xxx.it
Connection: keep-alive
Content-Length: 0
Accept: */*
Origin: http://xxx.it
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
X-API-Key: 1EF22FBACA1F1715BA3E70AE245A5BF4
Referer: http://xxx.it/test/ost-api-example.php
Accept-Encoding: gzip, deflate
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: OSTSESSID=jnie4mlmor9q9hpehbrfl388k3; cb-enabled=accepted


Any tips for me? Need more info?

Thanks

Comments

  • You're going to have issues, the API is locked down to the IP address, so you'd need a new API key for everyone.. or, just add a relay php script as your ajax endpoint.
  • Hi,

    im trying with the same API Key switching the code from php and ajax
  • Maybe share your scripts? Hard to say without looking.
  • Really easy code

    $(document).ready(function() {
    $.ajax({
    method: 'POST',
    headers: {
    'X-API-Key': '1EF22FBACA1F1715BA3E70AE245A5BF4'
    },
    json: {
    "name": "name",
    "email": "jonh@gmail.com",
    "subject": "subject",
    "message": "text",
    "topicId": "1",
    //"attachments": osTicketAttachments
    }
    },
    function (error, response, body) {
    if (error) {
    console.log(error);
    }
    else {
    console.log(response.statusCode, body);
    }
    })
    });


  • The 401 should only trigger for a bad API key.. if it was 400 you'd be blaming the data.. see the validate function here: https://github.com/osTicket/osTicket/blob/develop/include/api.tickets.php

    I'm intrigued by your syntax, I thought to send JSON you had to set the datatype to json, then convert the data dictionary to a JSON string, I've not seen just json: {} before. Does that work?
    I can't see it in the docs.

    You might need to change it to

    type: 'POST',
    data: JSON.stringify({
    "name": "name",
    "email": "jonh@gmail.com",
    "subject": "subject",
    "message": "text",
    "topicId": "1",
    }),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',

    But I haven't tested it.
  • Thanks so much. Now its working

    this is the code:

    $.ajax({
    type: 'POST',
    headers: {
    'X-API-Key': '94914EFD38C290653733F0FD538974A4'
    },
    data: JSON.stringify({
    "name": "name",
    "email": "e@mail",
    "subject": "subject",
    "message": "text",
    "topicId": "1",
    }),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    },
    function (error, response) {
    if (error) {
    console.log(error);
    }
    else {
    console.log(response.statusCode);
    }
    })


  • Awesome! Cheers for posting the complete solution mate.
  • Hello matteot 

    With this script, are you able to upload attachments to the ticket?

    Let me know
Sign In or Register to comment.