groovy-wslite icon indicating copy to clipboard operation
groovy-wslite copied to clipboard

Calling a REST put with JSON Content

Open sfrogner opened this issue 8 years ago • 3 comments

Thanks a bunch on your project as this is great. I was wonding if you could point me to proper format to include json in a PUT call. Apologies for rudimentary question does this look correct for the post call. I believe my json is malformed because getting a 400 but wanted to double-check this right approach as i stumbled through

@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'])

println(response.json.key)
println(response.json.id)
println(response.json.affinityToken)
println(response.json.clientPollTimeout)
def skey = response.json.key
def sid = response.json.id
def stoken = response.json.affinityToken
def builder = new JsonBuilder()
def prechat = []
def PreEnt = []

def root = builder {
    "organizationId" "00D28000000f5N9"
    "deploymentId" "572280000008R6L"
    "buttonId" "573D000000000OC"
    "agentId" "005B0000000F3b2"
    "doFallback" true
    "sessionId" sid
    "userAgent" "Lynx/2.8.8"
    "screenResolution" "2560x1440"
    "visitorName" "Gerald"
    "prechatDetails" prechat
    "prechatEntities" PreEnt
    "receiveQueueUpdates" true
    "isPost" true


}


println(builder.toPrettyString())

try {
    def responseI = client.post(path: '/Chasitor/ChasitorInit',
            headers: ['X-LIVEAGENT-API-VERSION': '34', 'X-LIVEAGENT-SESSION-KEY': skey, 'X-LIVEAGENT-AFFINITY': stoken,
                      'X-LIVEAGENT-SEQUENCE'   : '1', 'Content-Type': 'application/json; charset=UTF-8', 'Accept': 'application/json'])

            {
                text builder.toPrettyString()
            }

    return responseI.json
} catch(ex) {
    ex.printStackTrace()
}

sfrogner avatar May 13 '16 16:05 sfrogner

Updating as worked out the malform. Tried from postman so works there but was curious as the response is failing with the following

wslite.rest.RESTContentParseException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:80)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:271)
    at wslite.rest.RESTClient.buildResponse(RESTClient.groovy:129)
    at wslite.rest.RESTClient.this$2$buildResponse(RESTClient.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
    at wslite.rest.RESTClient.executeMethod(RESTClient.groovy:111)

sfrogner avatar May 13 '16 17:05 sfrogner

It looks like the problem is with the response and not the request. Try printing out the result of responseI.contentAsString and see what format the response is in.

jwagenleitner avatar May 13 '16 18:05 jwagenleitner

Hi John thanks for response.. When i tried your suggestion i was getting same error. Seems when building the response it errors.. I could comment out the return and id still get it.

When i run it from firefox httpRequester this is what is see

-- response -- 200 OK X-Cnection: close Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Cache-Control: no-cache Pragma: no-cache Expires: -1 Content-Type: application/json Content-Encoding: gzip Content-Length: 28

OK

When i look at there documentation at Salesforce i see formats JSON and response body none

https://developer.salesforce.com/docs/atlas.en-us.live_agent_rest.meta/live_agent_rest/live_agent_rest_ChasitorInit.htm

Thanks

sfrogner avatar May 14 '16 01:05 sfrogner