1

I have written a custom REST service in Salesforce as below:

@RestResource(urlmapping='/MyRestWS/*')
global class CustomRestWS
{
    @HttpGet
    global static String getName()
    {
        return UserInfo.getUserName();
    }
    @HttpPost
    global static String sayHi(String test)
    {
        return 'Hi '+test;
    }
}

Please clarify my doubts below:

  1. How to invoke it from outside, say from Postman or Java? From Salesforce docs, it is mentioned that "You can set up authorization using OAuth 2.0 or by passing a Session ID." How to authorize using Session ID?
  2. Is it mandatory to create a connected App in Salesforce if we want to invoke a Salesforce REST API? We cannot consume any salesforce REST API(even standard REST API) without creating a connected app?
  3. Which authentication is most commonly used?
  4. What is Basic Authentication, how to use Basic Authentication to invoke REST API?

Thanks in advance.

1

You can access it as

baseUrlofyourorg+/services/apexrest/MyRestWS

If you want to use Oauth flow then I think you need the connected app. You can also expose your web service publically using Site guest user profile then you don't need any authentication or connected app.

grant_type=password&client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82Hn
FVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCscA9GE&client_secret=
1955279925675241571&username=testuser%40salesforce.com&password=mypassword123456

this is how your request will look

A Basic curl example

for instance:

curl -d "grant_type=password&client_id={client_id}&client_secret={client_secret}&username=user@domain.com&password={Password}{SecurityToken}" "https://login.salesforce.com/services/oauth2/token"

... will return

{"id":"https://login.salesforce.com/id/xxxxxxxxxxxxx","issued_at":"1347503304385","instance_url":"https://ap1.salesforce.com","signature":"xxxxxxxxxxxxx","access_token":"XXXXtokenXXXXXX"

More detail can be found here or here

The Most commonly used Authentication is OAuth 2.0 and its globally accepted on all major sites.

In Basic authentication we pass Username and password in request and I don't think SF support this authentication in accesing REST services.

There is a very good step by step example provided here

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.