Support Topics Documentation Slack YouTube Blog

Custom service invoke in swift result in incomplete json received

I’m using latest version of swift sdk
I tried to post a dictionary to a custom service, however the backend can’t parse the data.

The dictionary look like this, with data extracted from a GeoPoint:
let dict : [String: Any] = ["latitude”:geoPoint.latitude, "longitude”:geoPoint.longitude, “categories”: geoPoint.categories, “metadata” : point.metadata as Any ]

The post method look like this:

private let serviceName = “postManager”

postPoint(name: dict, …)

func postPoint(name: Any , responseHandler: (( Any ?) -> Void)!, errorHandler: ((Fault) -> Void)!) {
Backendless.shared.customService.invoke(serviceName: serviceName, method: “postPoint”, parameters: name, responseHandler: responseHandler, errorHandler: errorHandler)
}

When I tried to parse the parameter in cloudcode, I get this: " Unexpected end of JSON input at JSON.parse…"
When I tried to directly print parameter without parsing, only “{” shows up.
If I pass a [[String:Any]] instead of [String: Any] from swift client, cloucode print log shows only “[{”

I suspect this might have something to do with “\” in swift JSONSerialization

I tried adding a backendless GeoPoint object directly as parameter to invoke custom service.

parameters = jsonUtils.objectToJSON(objectToParse: parameters) in private func invokeService()
doesn’t seem to give the right format

private func invokeService(serviceName: String, method: String, parameters: Any?, executionType: ExecutionType?, responseHandler: ((Any?) -> Void)!, errorHandler: ((Fault) -> Void)!) {
    var headers = ["Content-Type": "application/json"]
    if let executionType = executionType {
        headers["bl-execution-type"] = ExecutionTypeMethods.shared.getExecutionTypeValue(executionType: executionType.rawValue)
    }
    if var parameters = parameters {
        parameters = jsonUtils.objectToJSON(objectToParse: parameters)
        BackendlessRequestManager(restMethod: "services/\(serviceName)/\(method)", httpMethod: .post, headers: headers, parameters: parameters).makeRequest(getResponse: { response in
            self.processInvokeResponse(response: response, responseHandler: responseHandler, errorHandler: errorHandler)
        })
    }
    else {
        BackendlessRequestManager(restMethod: "services/\(serviceName)/\(method)", httpMethod: .post, headers: headers, parameters: nil).makeRequest(getResponse: { response in
            self.processInvokeResponse(response: response, responseHandler: responseHandler, errorHandler: errorHandler)
        })
    }
}

cloudcode console still just print “{”

Hello,

Could you please provide your Application Id?

Regards,
Olha

Hi, my id is FDB1D93A-FA62-CAE5-FF2D-539467AC5700

Thank you,
we’ll investigate this issue and answer as soon as possible.

Regards,
Olha

Hi @Mian_Wei

If I correctly understand your goal you want to send a dictionary to the server
let dict : [String: Any] = ["latitude”:geoPoint.latitude, "longitude”:geoPoint.longitude, “categories”: geoPoint.categories, “metadata” : point.metadata as Any ]

if so, you have to change your business logic a little bit, currently your API Method postPoint expects a String type but instead there must be an Object:

  /**
   * @param {Object} name
   */ 

also don’t forget to remove this line var obj = JSON.parse(name); because it’s already an object

Regards, Vlad

Hi Vladimir,

Thank you for the help. I think I know more clearly about how Backendless sdk work now :slight_smile:

Mian