function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sarvesh001sarvesh001 

Rest in apex

Hi ,

 

 I have created a simple REST API in Apex  through Your Name | Setup | Develop | Apex Classes | New  

 how to get  cURL for this.

 

thanks,

sarvesh.

Best Answer chosen by Admin (Salesforce Developers) 
SunilKumar.ax1648SunilKumar.ax1648

Hi Servesh,

 

you can call like that..

 

 

  String url = 'https://ap1.salesforce.com/services/apexrest/GetService';

All Answers

SunilKumar.ax1648SunilKumar.ax1648

Hi,

which method you are using get or post

 

for get u use like this.

 

@RestResource(urlMapping='/test/*')
global with sharing class testEndPoint{

@HttpGet
global static string doGet(){


return 'Swell';
}

}

 

for get u use like that and pass it like that

 

req.setEndpoint(instanceUrl+'/services/apexrest/test');

 

 

and post

 

 

@RestResource(urlMapping='/OneToAnother/*')
global class SalesforceAcc
{
public SalesforceAcc(){}

@HttpPost
global static string doPost(){
string jsonData = RestContext.request.requestBody.tostring();
//jsonData = jsonData.replace('"{','{').replace('}"','}').replace('"[','[').replace(']"',']').replace('\\','');
jsonData = jsonData.replace('Account','Account_c');

System.debug(' :: string::'+jsonData);

//getting the JSON and deserializing according the class structure
Account_c reqData = (Account_c)JSON.deserialize(jsonData, Account_c.class);
System.debug(' :: JSON :: '+ reqdata );

return reqdata.Id;
}

public class Account_c
{
string Id {get; set;}
string Name {get; set;}
}
}

sarvesh001sarvesh001

Hi Sunil,

thanks for replay and  my  code is below 

 

My target org code: 

My target org code: 

 

@RestResource(urlMapping='/GetService/*')  
global with sharing class getservice1
{      
     @HttpGet     
     global static String postRestMethod(RestRequest req, RestResponse res)     
     {          
          return 'Hi, You have invoked getservice1 created using Apex Rest and exposed using REST API';        
     }
}

 for the above code how to get url to add to remotesite  settings

 

i mean if our target code and source code in one orgnisation then i want  target url to add to reotesite

for ex like  'https://vikashtalanki-developer-edition.na12.force.com/services/apexrest/GetService how to get this type url for my target code.

 

please replay.

thanks,

 

 

SunilKumar.ax1648SunilKumar.ax1648

GO

Setup--->Sequrity Controls---->Remote Site Setting and put there name and https://ap1.salesforce.com thats it.

sarvesh001sarvesh001

Hi sunil ,

 

 meas for the above my target code  i can add  url   https://ap1.salesforce.com  in remote site settigs.

 

 

Thanks,

SunilKumar.ax1648SunilKumar.ax1648

Yes

or

can you tell me what types of error r u getting?

sarvesh001sarvesh001




My target org code: 

 

@RestResource(urlMapping='/GetService/*')  
global with sharing class getservice1
{      
     @HttpGet     
     global static String postRestMethod(RestRequest req, RestResponse res)     
     {          
          return 'Hi, You have invoked getservice1 created using Apex Rest and exposed using REST API';        
     }
}

 and my source org code is:

 

<apex:page controller="getservice2">
    <apex:form >
        <apex:commandButton action="{!getResult}" value="Submit"> </apex:commandButton>
        <br/><br/>        {!myresponse}
    </apex:form>
</apex:page>

 

public class getservice2
{
    public String myresponse{get;set;}
    public Pagereference getResult()
    {
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('GET');
        String url = 'https://vikashtalanki-developer-edition.na12.force.com/services/apexrest/GetService';
        req.setEndpoint(url);
        HTTPResponse resp = http.send(req);
        myresponse=resp.getBody();          

        return null;    }
}

 which on cliking submit button should give me 'Hi, You have invoked getservice1 created using Apex Rest and exposed using REST API'..........

 

 

 for te above code they are added url 'https://vikashtalanki-developer-edition.na12.force.com/services/apexrest/GetService'  in Remote Sites (Setup | Administration Setup | Security Controls | Remote Site Settings).

 

My dought is i have created the same thing in my org  but i did'n got the above type url to add to remote site settings

 

please suggest me .

 

 

 

SunilKumar.ax1648SunilKumar.ax1648

you can put this URL:-

 

 

https://ap1.salesforce.com

 

or when you click on button getting some MSG then follw the same it will give out put 

 

Please Accept my answer as solution (if it was helpful) to make it available to others for similar problem & Don't forget to give me KUDOS by Clicking on STAR icon

sarvesh001sarvesh001

Hi Sunil, sorry for truble ,

 

it sowig error when i mensioned the url: https://ap1.salesforce.com

 

my modified code is like below

 

public class getservice2
{
public String myresponse{get;set;}
public Pagereference getResult()
{
HttpRequest req = new HttpRequest();
Http http = new Http();
req.setMethod('GET');
String url = 'https://ap1.salesforce.com';
req.setEndpoint(url);
req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
HTTPResponse resp = http.send(req);
myresponse=resp.getBody();

return null; }
}

 

when i am clicking button it shows error.

i think url may be problem.

 

thanks,

 

 

 

 

 

 

 

SunilKumar.ax1648SunilKumar.ax1648

Hi Servesh,

 

you can call like that..

 

 

  String url = 'https://ap1.salesforce.com/services/apexrest/GetService';

This was selected as the best answer
SunilKumar.ax1648SunilKumar.ax1648

can you explain error also

sarvesh001sarvesh001

Hi Sunil,

 

Thank you so much .. its working.

thanks for giving solution to me.

 

 

Thanks,

Sarvesh.

sarvesh001sarvesh001

HI Sunil,

 

its working ,but i have created new site (Develop-->sites) and added that rest class to this site ,

i mean new site having target code then how can i call that one in same org.

 

please replay

thanks,

Sarvesh.

 

 

 

SunilKumar.ax1648SunilKumar.ax1648

Hello Servesh,

you can add your VF page in site and call it normally it will work

 

steps:-Setup---->Develop--->Sites--->Click on site level or edir----> site VF Pages Edit buttons and add UR VF page in 

 

now check it will work

sarvesh001sarvesh001

Hi Sunil,

 

I have added  rest class to site add tried to call that but it shows error 
[{"message":"HTTPS Required","errorCode":"UNSUPPORTED_CLIENT"}]

 

please suggest

 

thanks

sarvesh001sarvesh001

Hi Sunil,

 

I have added  rest class to site add tried to call that but it shows error 
[{"message":"HTTPS Required","errorCode":"UNSUPPORTED_CLIENT"}]

 

please suggest

 

thanks

SunilKumar.ax1648SunilKumar.ax1648

Hello Servesh,

you need to enabled your Apex class.

Enabled Apex Class Access

 

how to enable the Apex also you can give the access of object and VF and Apex classes.

 

Step:- Setup--->Develop---->Sites------>Edit-------->Click on public Access Settings----> Go Section for enabled Apex classes Access -----> Edit buttons and Add your Apex classes.

sarvesh001sarvesh001

Hi Sunil,

 

I have added my apex class contaied rest code to sites through  Setup--->Develop---->Sites------>Edit-------->Click on public Access Settings, but i have got same error. 

my apex class that i have added to site is below

 

@RestResource(urlMapping='/GetService/*')  
global with sharing class getservice1
{      
     @HttpGet     
     global static String postRestMethod()     
     {          
          return 'Hi, You have invoked getservice1 created using Apex Rest and exposed using REST API';        
     }
}

 

my apex page source code is

<apex:page controller="getservice2">
<apex:form >
<apex:commandButton action="{!getResult}" value="Submit"> </apex:commandButton>
<br/><br/> {!myresponse}
</apex:form>
</apex:page>

 

public class getservice2
{
public String myresponse{get;set;}
public Pagereference getResult()
{
HttpRequest req = new HttpRequest();
Http http = new Http();
req.setMethod('GET');
String url = 'http://sarveswararao-developer-edition.ap1.force.com/services/apexrest/GetService ';
req.setEndpoint(url);
req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
HTTPResponse resp = http.send(req);
myresponse=resp.getBody();
return null; }
}

 

plesae suggest me.

 

SunilKumar.ax1648SunilKumar.ax1648

you have create any namespace prefix or package if you have then simply copy that namespace prefix and add it below location

 

 

String url = 'http://sarveswararao-developer-edition.ap1.force.com/services/apexrest/HERE namespace prefix/GetService ';

 

 

where i put you can put ur namespace

 

 

HERE namespace prefix = ur namespace

 

It was due to a permissions error it seems. Somehow an object for the services profile had an impossible permission setup (it had full read write modify all on an child object where it did not have read write modify all on the parent object (an opportunity)). So fixing the permissions and making sure the service had read/write to all objects and fields it required seems to have fixed this error.

 

 

sarvesh001sarvesh001

HI Sunil,

 

Sorr for trouble

 I have create names prifix and use that in url but same error happen.

my url is:String url = 'http://sarveswararao-developer-edition.ap1.force.com/services/apexrest/ysr/GetService '; 

 

Here i have foud some inforation please check  http://salesforce.stackexchange.com/questions/6122/custom-rest-service-https-error

 

thanks,

sarvesh. 

sarvesh001sarvesh001

HI Sunil ,

GMorning

 

its working...  by below 

 changes

 

  1. Log into Salesforce.com.
     
  2. Click Setup, click Security Controls, and then click Session Settings.
     
  3. Uncheck the option labeled Require secure connections (HTTPS).

 Thanks,

Sarvesh.