2

I have requirement of updating Zendesk Tickets using Groovy HTTP Builder. I use the following code

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import java.util.Properties;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import groovyx.net.http.*;
import static groovyx.net.http.Method.*;
import groovy.json.*;
import groovyx.net.http.ContentType;

def jsonBuilder = new groovy.json.JsonBuilder();
class MyTicket
{
    def subject
}

        def myTicket = new MyTicket( 
        subject: 'xyz'.toString()
        )
def ticketList=[myTicket]
jsonBuilder(ticket:ticketList)


println(jsonBuilder)

def authSite = new HTTPBuilder('https://{subdomain}.zendesk.com/api/v2/tickets/{ticketid}.json');
authSite.auth.basic 'username', 'password';
authSite.request( Method.PUT, ContentType.JSON )
 {  req ->
 uri.path = ''https://{subdomain}.zendesk.com/api/v2/tickets/{ticketid}.json'';
                requestContentType = ContentType.JSON;
                headers.Accept = 'application/json';
                body =[jsonBuilder]

                response.success = { resp, reader->
                reader.ticket.subject;
    }
}   

But the ticket is not being updated. Is there any kind of execute method. Kindly Suggest me where I went wrong.

Shashank Cool
  • 91
  • 2
  • 11
  • Any errors? What's the json look like? Is that ok? It's hard to see what's going on, the code's a bit of a mess :-( – tim_yates Jan 21 '14 at 09:39
  • @tim_yates : Initially I am building JSON and sending it through body. No Errors . It is in the form {"ticket":[{"subject":"xyz"}]} . But the value is not being updated. – Shashank Cool Jan 21 '14 at 09:43
  • If I use the content type as URLENC I am getting following error ava.lang.NullPointerException: Cannot get property 'subject' on null object – Shashank Cool Jan 21 '14 at 09:54

1 Answers1

5

Try this, you'll need to set up your subdomain, ticketid, user and pass (I've removed all the unnecessary imports as well):

@Grab( 'org.codehaus.groovy.modules.http-builder:http-builder:0.6' )
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.PUT
import static groovyx.net.http.ContentType.JSON

def subdomain = 'woo'
def ticketid  = '123'

def authSite = new HTTPBuilder("https://${subdomain}.zendesk.com/api/v2/tickets/${ticketid}.json");
authSite.auth.basic( 'user', 'pass' )
authSite.request( PUT, JSON ) { req ->
    body = [ ticket:[ subject: 'xyz' ] ]

    response.success = { resp, json ->
        println "Success! ${resp.status}"
    }

    response.failure = { resp ->
        println "Request failed with status ${resp.status}"
    }
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I executed the following code but encountered with following error Request failed with status 404 – Shashank Cool Jan 21 '14 at 10:01
  • @ShashankCool So the URL `"https://${subdomain}.zendesk.com/api/v2/tickets/${ticketid}.json"` doesn't exist. Are you sure you set the subdomain and ticketid correctly? Are you sure that's the correct URL? Try printing it out and going to the printed URL in a browser... Does that work? – tim_yates Jan 21 '14 at 10:04
  • @ShashankCool Looking at [the documentation](http://developer.zendesk.com/documentation/rest_api/tickets.html), I believe the URL should be: `"https://${subdomain}.zendesk.com/api/v2/tickets.json"` no? Updated the answer to reflect this... – tim_yates Jan 21 '14 at 10:05
  • @ShashankCool Are you trying to create a ticket, or update an existing one? – tim_yates Jan 21 '14 at 10:09
  • I am trying to update the existing ticket @tim_yates – Shashank Cool Jan 21 '14 at 10:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45696/discussion-between-shashank-cool-and-tim-yates) – Shashank Cool Jan 21 '14 at 10:13
  • @ShashankCool Right, so [the documentation](http://developer.zendesk.com/documentation/rest_api/tickets.html#updating-tickets) shows you need to use `PUT`, not `POST`... Updated answer – tim_yates Jan 21 '14 at 10:13
  • In the initial code I used PUT method but the ticket is not being updated – Shashank Cool Jan 21 '14 at 10:17
  • @ShashankCool My mistake :-( What about my code above? Does it tell you why it is failing? – tim_yates Jan 21 '14 at 10:17
  • I executed your code and encountered with Request failed with status 500 – Shashank Cool Jan 21 '14 at 10:19
  • 1
    @ShashankCool that's closer... Internal Server error. Did you try with the new body `body = [ ticket:[ subject: 'xyz' ] ]` (which I believe is right from looking at the docs)? – tim_yates Jan 21 '14 at 10:21
  • 1
    @ShashankCool Cool! Got there in the end ;-) Have fun! – tim_yates Jan 21 '14 at 10:25