Answered

Update a Ticket in VB

I have developed a number of links to FreshDesk API including Create Customer, Create Ticket, Read Companies / Contacts, Read Tickets but struggling to get Update Ticket to work.


Here is my Code


 

 

 

    Public uRequestUri As String = "https://xxx.freshdesk.com"
    Public sAPIKey As String = "[Our API Key]"
    Public uTickets As List(Of FreshDesk_Ticket)
    Public Class FreshDesk_Ticket
        Public id As Integer
        Public attachments() As Object
        Public cc_mails() As String
        Public company_id As String
        Public custom_fields As Object
        Public deleted As Boolean
        Public description As String
        Public description_text As String
        Public due_by As Date
        Public email As String
        Public email_config_id As String
        Public facebook_id As String
        Public fr_due_by As Date
        Public fr_escalted As Boolean
        Public fwd_emails() As String
        Public group_id As String
        Public is_escalated As Boolean
        Public name As String
        Public phone As String
        Public priority As Integer
        Public reply_cc_emails() As String
        Public requester_id As String
        Public responder_id As String
        Public source As String
        Public spam As Boolean
        Public status As Integer
        Public subject As String
        Public tags() As String
        Public to_emails() As String
        Public twitter_id As String
        Public type As String
        Public created_at As Date
        Public updated_at As Date
    End Class

Dim iJob = Integer = 7815 ' (This is an existing Ticket)
            Dim uTicket = New FreshDesk_Ticket
            With uTicket
                .status = 5
            End With
            Dim sResult As String = JsonConvert.SerializeObject(uTicket)

            Dim uFDConnection = New RestClient(uRequestUri)
            uFDConnection.Authenticator = New HttpBasicAuthenticator(sAPIKey, "")
            uFDConnection.AddDefaultHeader("Accept", "application/json")
            uFDConnection.AddDefaultHeader("Content-Type", "application/json; charset=utf-8")

            Dim uRequest = New RestRequest("/api/v2/tickets/" & iJobID.ToString, Method.POST)
            uRequest.RequestFormat = DataFormat.Json
            uRequest.AddHeader("Accept", "application/json")
            uRequest.AddHeader("Content-Type", "application/json")
            uRequest.AddParameter("application/json", sResult, ParameterType.RequestBody)

            Dim uResponse As RestResponse = uFDConnection.Execute(uRequest)

 

 

 It fails with the following error:

"StatusCode: MethodNotAllowed, Content-Type: application/json; charset=utf-8, Content-Length: 95)" Content: "{""message"":""POST method is not allowed. It should be one of these method(s): GET, PUT, DELETE""}" ContentEncoding: "" ContentLength: 95 ContentType: "application/json; charset=utf-8" Cookies: Count = 1 ErrorException: Nothing ErrorMessage: Nothing Headers: Count = 16 RawBytes: {Length=95} Request: {RestSharp.RestRequest} ResponseStatus: Completed {1} ResponseUri: {https://xxx.freshdesk.com/api/v2/tickets/7815} Server: "" StatusCode: MethodNotAllowed {405} StatusDescription: "Method Not Allowed"


If I remove the / between tickets and 7815 (the ticket number) I get this

"StatusCode: NotFound, Content-Type: application/json; charset=utf-8, Content-Length: -1)" Content: " " ContentEncoding: "" ContentLength: -1 ContentType: "application/json; charset=utf-8" Cookies: Count = 1 ErrorException: Nothing ErrorMessage: Nothing Headers: Count = 15 RawBytes: {Length=1} Request: {RestSharp.RestRequest} ResponseStatus: Completed {1} ResponseUri: {https://xxx.freshdesk.com/api/v2/tickets7815} Server: "" StatusCode: NotFound {404} StatusDescription: "Not Found"


Can anyone stop my error ?

 



Best Answer

 Hi Angus Kerr,


You have used HTTP method POST for updating which you have to change it to PUT. We have provided this information as a part of the error message you have received.


Please change the uRequest initialization as follows,


Dim uRequest = New RestRequest("/api/v2/tickets/" & iJobID.ToString, Method.PUT)


Thanks,

Ganesh


Answer

 Hi Angus Kerr,


You have used HTTP method POST for updating which you have to change it to PUT. We have provided this information as a part of the error message you have received.


Please change the uRequest initialization as follows,


Dim uRequest = New RestRequest("/api/v2/tickets/" & iJobID.ToString, Method.PUT)


Thanks,

Ganesh

Thanks Ganesh.


Made the change and then got a "Bad Request". It didn't like my Request Data so simplified my Ticket Definition and got it working.

Here is the final code if it helps anyone.


Angus 

Public uRequestUri As String = "https://xxx.freshdesk.com"
    Public sAPIKey As String = "[Our API Key]"
    Public uTickets As List(Of FreshDesk_UpdateTicket)
    Public Class FreshDesk_UpdateTicket
        Public status As Integer
    End Class
 
Dim iJob = Integer = 7815 ' (This is an existing Ticket)
            Dim uTicket = New FreshDesk_UpdateTicket
            With uTicket
                .status = 5
            End With
            Dim sResult As String = JsonConvert.SerializeObject(uTicket)
 
            Dim uFDConnection = New RestClient(uRequestUri)
            uFDConnection.Authenticator = New HttpBasicAuthenticator(sAPIKey, "")
            uFDConnection.AddDefaultHeader("Accept", "application/json")
            uFDConnection.AddDefaultHeader("Content-Type", "application/json; charset=utf-8")
 
            Dim uRequest = New RestRequest("/api/v2/tickets/" & iJobID.ToString, Method.PUT)
            uRequest.RequestFormat = DataFormat.Json
            uRequest.AddHeader("Accept", "application/json")
            uRequest.AddHeader("Content-Type", "application/json")
            uRequest.AddParameter("application/json", sResult, ParameterType.RequestBody)
 
            Dim uResponse As RestResponse = uFDConnection.Execute(uRequest)

 


HI Angus

Could you be so kind and help with a full code to retrieve all tickets , I'm using VB.2010..



Regards

David

Login or Signup to post a comment