+ Start a Discussion
agrassiagrassi 

ForceTK - Request parameters not being passed

Hi all. I'm having a problem accessing the REST API from a VF page. Basically, the request performs OK, except for the parameters, which are not being set/do not arrive on the endpoint.

 

I'm using forcetk to avoid the cross domain issue. This is the relevant VF code:

 

<apex:page showHeader="false" sidebar="false" standardStylesheets="false">

    <apex:includeScript value="{!URLFOR($Resource.MyResource, 'jquery-1.9.1.min.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.MyResource, 'forcetk.js')}"/>
    
    <script type="text/javascript">
        $(document).ready(function() {

            var forceTKClient = new forcetk.Client();
            forceTKClient.setSessionToken('{!$Api.Session_ID}');
            
            forceTKClient.apexrest(
                '/MyRestResource',
                function(data, textStatus, jqXHR) {
                    console.log('SUCCESS - ' + data);
                },
                function(jqXHR, textStatus, errorThrown) {
                    console.log('ERROR - ' + textStatus);
                },
                'GET',
                {'myParameter':'myValue'},
                null,
                false
            );
        });
    </script>
    
</apex:page>

 

And this is the Apex controller for the REST resource:

 

@RestResource(urlMapping='/MyRestResource')
global with sharing class MyRestResource {
    
    @HttpGet
    global static Map<String, String> doGet() {

        Map<String,String> res = new Map<String,String> {
            'received' => 'yes'
        };      
        
        res.putAll(RestContext.request.params);
        return res;
    }

}

 

My custom REST method is reached correctly, and the response from it is returned correctly, but the parameters do never get to my custom REST method. I.e., the response received on the JS callback is

 

{received: "yes"}

 whereas it should be

 

{received: "yes", myParameter: "myValue"}

Inspecting the request sent from the browser I see that the endpoint is

 

https://c.na11.visual.force.com/services/proxy?_=1368023886976

 

 

"myParameter" isn't present anywhere on the browser request.

 

Any hint?

 

Thanks in advance,

Antonio

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Pat PattersonPat Patterson

I think apexrest() is expecting a string, rather than an object for the payload parameter. Pass the payload as JSON.stringify({'myParameter':'myValue'}) and it should work.

 

Cheers,

 

Pat

All Answers

May the Vforce be with youMay the Vforce be with you

Are you encoding your parameters before passing them to the http ?

agrassiagrassi

As long as I understand (looking at the source of forcetk.js) the parameters are a JS object that is passed directly to JQuery's .ajax method. I'm not encoding these.

agrassiagrassi

As long as I understand (looking at the source of forcetk.js) the parameters are a JS object that is passed directly to JQuery's .ajax method. I'm not encoding these.

Pat PattersonPat Patterson

I think apexrest() is expecting a string, rather than an object for the payload parameter. Pass the payload as JSON.stringify({'myParameter':'myValue'}) and it should work.

 

Cheers,

 

Pat

This was selected as the best answer
agrassiagrassi

Thanks Pat!