jwagenleitner / groovy-wslite

Lightweight SOAP and REST webservice clients for Groovy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling a REST put with JSON Content

sfrogner opened this issue · comments

commented

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()
}
commented

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)

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.

commented

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