3

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: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>

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,

  • 1
    What is the whole point of creating the REST Class? are you trying to render data on the VF page, have you looked at Jscript remoting as an alternative? – Rao May 9 '13 at 18:01
  • boards.developerforce.com/t5/APIs-and-Integration/… are u the same guy @rich rosen – Rao May 9 '13 at 20:19
1

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.