TransWikia.com

Error 500 when using Live Agent REST API with prechatDetails

Salesforce Asked on November 19, 2021

Hello I am developing an iOS app allowing to chat with a service agent using live agent.
The app is using Live Agent REST API.

Everything was working fine till I added prechatDetails to the ChasitorInit.
When I populate prechatDetails property in the request, I get a response with a 500 error:

{ URL: https://d.la1-c1-lon.salesforceliveagent.com/chat/rest/Chasitor/ChasitorInit } { status code: 500, headers {
    "Access-Control-Allow-Credentials" = true;
    "Access-Control-Allow-Origin" = "*";
    "Cache-Control" = "no-cache";
    "Content-Encoding" = gzip;
    "Content-Length" = 319;
    "Content-Type" = "application/json";
    Expires = "-1";
    Pragma = "no-cache";
    "X-Cnection" = close;
} }

My code is:

let prechatDetails: [[String : AnyObject]] = [
                ["label": "First Name", "value": userData[RegistrationViewController.Keys.FirstName]!, "transcriptFields": ["FirstName__c"], "displayToAgent": true],
                ["label": "Last Name", "value": userData[RegistrationViewController.Keys.LastName]!, "transcriptFields": ["LastName__c"], "displayToAgent": true]
            ]

            let jsonBody : [String : AnyObject] = [
                JSONRequestBodyKeys.OrganizationId: AppDelegate.OrganizationId,
                JSONRequestBodyKeys.DeploymentId: AppDelegate.DeploymentId,
                JSONRequestBodyKeys.ButtonId: discussion.chatButton.buttonId,
                JSONRequestBodyKeys.SessionId: discussion.sessionId,
                JSONRequestBodyKeys.VisitorName: userData[RegistrationViewController.Keys.FirstName]!,
                JSONRequestBodyKeys.ReceiveQueueUpdates: true as Bool,
                JSONRequestBodyKeys.IsPost: true as Bool,

                "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36",
                "language": "en-US",
                "screenResolution": "2560x1440",
                "prechatDetails": prechatDetails,
                "prechatEntities": []

            ]
            do {
                request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(jsonBody, options: .PrettyPrinted)
            }

If I replace the first line by prechatDetails: [[String : AnyObject]] = [], everything works fine.
It does not seems related to the JSON as a wrong JSON returns a 400 error.

Is anybody able to help? I am out of idea to solve this.

Thanks in advance,

Alex

One Answer

NSJSONSerialization is an Objective-C API that you are calling into from Swift. That's okay, but you should not expect it to properly serialize Swift objects. You should replace Swift's Bool type with NSNumber(bool:), double-check that your other values have Obj-C bridged equivalents (is userData[RegistrationViewController.Keys.LastName]! always a String? Are you sure?), and be aware that you're dancing madly on the lip of a volcano as long as you're force-unwrapping optionals all over the place in your serialization code.

Edit: Does this sound like an iOS user agent to you? ?

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"

Edit 2: I would also like an iOS device with screen resolution 2560x1440, while you're at it!

Answered by Jonathan Hersh on November 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP