Question

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.

Was it helpful?

Solution

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}"
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top