Question

In iOS, I am calling a rest resource that is hosted on Salesforce:

@HttpPost
    global static String getEmail(String app, String BorrowerId, String BorrowerName) {
        System.debug('hit the web service');
        System.debug(app);
        System.debug(BorrowerId);
        System.debug(BorrowerName);

        JSONGenerator gen = JSON.createGenerator(true);
        gen.writeStartObject();
        gen.writeStringField('email', 'woohoo');
        gen.writeEndObject();

        String jsonString = gen.getAsString();
        System.debug(jsonString);
        return jsonString;
    }

In which the returned jsonString is: { "email" : "woohoo" }

As shown in the debug log. However, when I try to receive this response in iOS:

- (void)request:(SFRestRequest *)request didLoadResponse:(id)jsonResponse
{
    NSLog(@"json response: %@", jsonResponse);
    NSString *retString = [NSString stringWithFormat:@"%@", jsonResponse];
    NSLog(@"records: %@", retString);
}

I receive an error: WARNING error parsing json: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xcbaf230 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

I also tried to return an object in salesforce, but a same error is returned. How can I read the data back as an NSString?

Thanks

Was it helpful?

Solution

Have the web service return a Map.

@HttpPost
global static Map<String, String> getEmail(String app, String BorrowerId, String BorrowerName) {
    System.debug('hit the web service');
    System.debug(app);
    System.debug(BorrowerId);
    System.debug(BorrowerName);

    Map<String, String> emailData = new Map<String, String>{'email' => 'woohoo'};
    return emailData;
}

This allows you to treat the response as an NSDictionary

- (void)request:(SFRestRequest *)request didLoadResponse:(id)jsonResponse {
   NSString *emailString = [jsonResponse objectForKey:@"email"];
   NSLog(@"email: %@", emailString);
}

POST requests should be reserved for creating records, so you may consider re-doing this as a GET.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top