带有json有效负载的Groovy ws-lite REST post

时间:2016-05-13 07:45:32

标签: groovy

我试图通过ws-lite调用REST API。获得很好的工作,但调用json有效负载不起作用的帖子。一点点菜鸟,但是从github注意到wiki我不确定https://github.com/jwagenleitner/groovy-wslite

@Grab('com.github.groovy-wslite:groovy-wslite:1.1.2')
import wslite.rest.*
import groovy.json.JsonBuilder
def client = new    RESTClient("https://d.la10.salesforceliveagent.com/chat/rest")
def response = client.get(path:'/System/SessionId', headers:['X-LIVEAGENT-    API-VERSION':'36','X-LIVEAGENT-AFFINITY':'null'])

def skey = response.json.key
def sid = response.json.id
def stoken = response.json.affinityToken


client.post(path:'/Chasitor/ChasitorInit', headers:['X-LIVEAGENT-API-VERSION':'36','X-LIVEAGENT-SESSION-KEY':skey,'X-LIVEAGENT-AFFINITY':stoken,'X-LIVEAGENT-SEQUENCE':'1'])
{
    type ContentType.application/json
         json {
             "organizationId": "00D28000000f5N9",
             "deploymentId": "572280000008R6L",
             "buttonId": "573D000000000OC",
             "agentId": "005B0000000F3b2",
             "doFallback": true,
             "sessionId": sid,
             "userAgent": "Lynx/2.8.8",
             "screenResolution": "2560x1440",
             "visitorName": "Jon A",
             "prechatDetails": [],"prechatEntities": [],
             "receiveQueueUpdates": true,
             "isPost": true
         }
}

好奇,如果有人通过groovy中的ws-lite使用REST的帖子。我只是根据json格式得到编译失败。任何洞察力赞赏

1 个答案:

答案 0 :(得分:0)

json块中的post方法期望Map作为参数,因此以下方法可行(基本上将大括号更改为括号):

....
....
client.post(path:'/Chasitor/ChasitorInit', headers:['X-LIVEAGENT-API-VERSION':'36','X-LIVEAGENT-SESSION-KEY':skey,'X-LIVEAGENT-AFFINITY':stoken,'X-LIVEAGENT-SEQUENCE':'1'])
{
    type ContentType.application/json
        json ["organizationId": "00D28000000f5N9",
             "deploymentId": "572280000008R6L",
             "buttonId": "573D000000000OC",
             "agentId": "005B0000000F3b2",
             "doFallback": true,
             "sessionId": sid,
             "userAgent": "Lynx/2.8.8",
             "screenResolution": "2560x1440",
             "visitorName": "Jon A",
             "prechatDetails": [],"prechatEntities": [],
             "receiveQueueUpdates": true,
             "isPost": true]
}

由于Map是唯一的参数,因此您也应该省略括号。