0

I created a REST web service as below:

@RestResource(urlMapping='/v1/accounts/*')
global with sharing class REST_Account_Service_V1{

    @HttpGet
    global static Account doGet(){
       RestRequest req=RestContext.request;
       Account result=[select Id,Name,Phone,Website,AccountNumber FROM Account limit 1];
       return result;
      } 
}

I have tested it with workbench and it is working fine. Now I want to consume this REST service in .Net application, the URL of my rest service is as below: https://ap5.salesforce.com/services/apexrest/v1/accounts

When I browse this URL on a browser, I got the below error:

enter image description here

I tried to consume that service in SOAP UI but it is showing the error session expired. I also tried to consume that service in console application in .Net but it is asking for username and password. I provided my salesforce username and password but no luck.I also tried with my system username and password but again no luck.

Please tell me the steps to consume REST service outside salesforce and within salesforce.

Thanks in advance !

1
  • 1
    I believe that you'd need to implement some authentication - to obtain the session id and put in the request as Authentication header for example.
    – vortex
    Commented Jun 17, 2017 at 9:27

1 Answer 1

2

When you accessed your service through the Workbench you had already logged in and the Workbench included the session id (that is used to confirm who is making the request) for you. When accessing the service from external code, you first need to authenticate yourself to get a session id and then include the session id in each request. The latter is simple; you just need to include a Authorization header that includes the session id.

The former - the authentication mechanism - is more complicated. Some relevant documentation on how to authenticate is Force.com REST API Developer Guide - Understanding Authentication. Read through that carefully as there are a number of choices to be made.

4
  • Thanks Keith for the response.I think session id is valid for a particular period, after that period a new session id is created. So every time we have to provide a new session id to client.I think in real time scenario it will not accepted. Please correct me if I am wrong. Commented Jun 20, 2017 at 10:55
  • 1
    @RaamKothiyal If you want to cache the session id (and the time it was issued) to avoid the overhead of connecting every time you could. All depends on how frequent the requests are and how many are made at once. If the requests are rare (say once a day) and all at once then connect, call, disconnect makes sense. If they are every few seconds or minutes then caching and re-using makes sense.
    – Keith C
    Commented Jun 20, 2017 at 11:07
  • Could you provide any code in .net where I can call rest api made on saleforce via authentication? Commented Jun 20, 2017 at 12:05
  • 1
    @RaamKothiyal I can't but Google can e.g. Consuming Force.com SOAP and REST Web Services from .NET Applications.
    – Keith C
    Commented Jun 20, 2017 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .