gpt4 book ai didi

groovy - 使用 Groovy 的 HTTP Builder 使用 PUT 更新 JSON 数据

转载 作者:行者123 更新时间:2023-12-02 21:36:35 25 4
gpt4 key购买 nike

我需要使用 Groovy HTTP Builder 更新 Zendesk Tickets。我使用以下代码

@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;
}
}

但是票证尚未更新。有没有一种执行方法。请建议我哪里出错了。

最佳答案

试试这个,您需要设置您的子域名ticketid用户密码(我也删除了所有不必要的导入):

@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}"
}
}

关于groovy - 使用 Groovy 的 HTTP Builder 使用 PUT 更新 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21254217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com