2

I'm new to salesforce and learning how to write and call Apex REST api.Here is a sample code on which I'm working.

@RestResource(urlMapping='/helloWorld/*')
global class RestWebservice {

  @HttpGet
  global static String helloWorld(){
    return 'HelloWorld';
  }

}

I've also set remote site URL in my org "https://akashdalvi-dev-ed.my.salesforce.com" (I've created this as my domain)and I'm getting the following error. enter image description here

Error image

Please can anyone share their valuable thoughts and share their knowledge?

Thanks in advance.!! :)

3
  • Did you authorize your application which is calling REST Api. You can refer developer.salesforce.com/page/… it has very good explanation.
    – San
    Jul 31 '15 at 6:42
  • @San : Thank you for your quick response.Now I've created one connected app to validate my above helloworld program.I've gone through the article and it's helpful.But in there case they have written the code in java to authenticate the app.If I'd get an apex code to authenticate my above program would be helpful.
    – Akash
    Jul 31 '15 at 7:03
  • You can refer salesforce.stackexchange.com/questions/46074/…
    – San
    Jul 31 '15 at 7:08
1

@Akash, you already have creadted valid & simple rest webservice RestWebservice class, issue is here why you are getting this INVALID_SESSION_IDerror when you are accessing this rest webserive using browser url https://akashdalvi-dev-ed.my.salesforce.com/services/apexrest/helloworld.

As you have directly called this get rest request without passing athorization session id in header of request. This rest webservice is secured by salesforce authentication, this is not open to everyone to access. only you can call it using Authorization token in header which is same doing by workbench as shown in above @parth Thakkar.

Note: Pass the request with valid Authorization bearer token in header of request if you are making any rest call to your salesforce instance rest webservice.

Let me know if you want any further clarification on this.

1
  • @Ajay : Thank you very much for your valuable knowledge sharing, could you please elaborate how should I make a sucessful call and how can I get the valid Authorization bearer token?if you can share any dummy format to call the salesforce instance rest webservice it would be helpfull for others as well and easy to understand.
    – Akash
    Jul 31 '15 at 10:07
0

this is small demo. for more detail Refer

http://salesforce-parth.blogspot.in/2015/07/how-to-use-insert-account-using-rest-in.html

@RestResource(urlMapping='/*')     
global class InsertAccount
{

@Httppost
global static String InsertAccount()
{  


    // Insert Account Using Post Method


    Account  acc = new Account();
    String jsonStr = null;

    if (null != RestContext.request.requestBody){

    jsonStr = RestContext.request.requestBody.toString();
    Map<String, object> m = (Map<String, object>)JSON.deserializeUntyped(jsonStr );
    system.debug('******'+m );
    acc.Name=(String)m.get('AccountName');
    insert acc;
   }
    return 'Account Inserted';
}


 @HttpGet
global static String InsertAccountRest()
{   

           // Insert Account Using Get Method

    Account  acc1 = new Account();
    acc1.Name=RestContext.request.params.get('AccountName');
    insert acc1;

    return 'Account Inserted';
}

}

For Executing the REST API Please do following steps

Go to this Link > https://workbench.developerforce.com/login.php

Select your Environment and API Version then checked the terms of service

Then click on Login With Salesforce
0

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.