Skip to main content

Hello,


I'm trying to create new ticket via ajax in coffeescript. I keep gettin "Status: 200 OK. Response: "{require_login":true"}" as response, eventhough no ticket was created.


My code:


$scope.sendSupportMail =() ->
data =
'{
"helpdesk_ticket":{
"description":"Test description",
"subject":"Test subject",
"email":"john@doe.com",
"priority":1,
"status":2
}
}'
$.ajax
url: 'https://mycompany.freshdesk.com/helpdesk/tickets.json'
type: 'POST'
contentType: "application/json; charset=utf-8"
dataType: "json"
APIkey: 'here goes the withAppKey'
data: data

success: (data, textStatus, jqXHR) ->
alert( 'Status: ' + jqXHR.status + ' ' + jqXHR.statusText + '. ' + 'Response: ' + jqXHR.responseText )
error:(jqXHR, tranStatus, errorThrown)->
alert( 'Status: ' + jqXHR.status + ' ' + jqXHR.statusText + '. ' + 'Response: ' + jqXHR.responseText )


Situation is the same, when I use username and password. Any idea what could be wrong? Thank you very much for any suggestion.



It appears that the Auth header is not being set. It needs to be set explicitly. The information in the following link may prove useful - http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax/5507289#5507289




Dear Rohit, thanks for an useul link! I'm able to craete a ticket now, but only with usage of username+password. I'd like to use apikey because of the security reasons. Nevertheless I didn't find out, how to make it work with the apikey instead of username and passowrd. Any idea, how to edit the code to make it work?




$scope.sendSupportMail =() ->
data =
'{
"helpdesk_ticket":{
"description":"Test description",
"subject":"Test subject",
"email":"john@doe.com",
"priority":1,
"status":2
}
}'

$.ajax
url: 'https://mycompany.freshdesk.com/helpdesk/tickets.json'
type: 'POST'
contentType: "application/json; charset=utf-8"
dataType: "json"
APIkey: 'apikey'
data: data
headers: 'Authorization': 'Basic ' + btoa('username' + ':' + 'password')

success: (data, textStatus, jqXHR) ->
alert( 'Status: ' + jqXHR.status + ' ' + jqXHR.statusText + '. ' + 'Response: ' + jqXHR.responseText )
error:(jqXHR, tranStatus, errorThrown)->
alert( 'Status: ' + jqXHR.status + ' ' + jqXHR.statusText + '. ' + 'Response: ' + jqXHR.responseText )



The standard format for authenticating with the API key is to use the format APIKEY:X where APIKEY is your API key and X is a dummy password (There is no password required, but we use a dummy one to maintain the format used when you authenticate via username:password).


If that is not working for you, I will need to check with my engineers and get back to you.




Well the thing is, that I don't want to get authenticated via username+password, but with apikey only - that's it's purpose, isn't it? Is there such possibility to use for authentication apikey only, or do I always have to use username+password?




Yes you can authenticate with just the API key, the format is the one mentioned in my last post. The format is <api_key>:<dummy_password>. So for example if your API key is "a1jf6t95", you will have to use "a1jf6t95:X" to authenticate where "X" is a dummy password (not your real password).




Oh, now it's finally clear! I've thought the whole time, that there has to be pair apiKey:apiKeyValue as another parameter in the http request. I paste the working code, in case anybody else would need it in the future. Thanks a lot for your help and patience Rohit.


$scope.sendSupportMail =() ->
user = userService.currentUser()
data =
'{
"helpdesk_ticket":{
"description":"'+$scope.description+'",
"subject":"'+$scope.subject+'",
"email":"'+user.get('email')+'",
"priority":1,
"status":2
}
}'

$.ajax
url: 'https://myCompany.freshdesk.com/helpdesk/tickets.json'
type: 'POST'
contentType: "application/json; charset=utf-8"
dataType: "json"
data: data
headers: 'Authorization': 'Basic ' + btoa('myAPIKey' + ':' + 'X')