Saturday, 27 June 2015

How to call rest api webservices

Step 1 : Initially create connected app in salesforce.
here:
callback URL: https://login.salesforce.com/services/oauth2/callback
selected OAuthScope:Full access (full)

Now you can use consumer key, consumer secret for rest api.

Step 2: How to get access token:

Method:POST
URL:https://login.salesforce.com/services/oauth2/token
client_id:consumer Key from salesforce connected app
client_secret:Consumer secret from salesforce connected app
grant_type:password
username:restAPi@soliant.com (user id)
password:password + security token



Output
{
    "id": "https://login.salesforce.com/id/.....",
    "issued_at": "1435335550081",
    "token_type": "Bearer",
    "instance_url": "https://ap2.salesforce.com",
    "signature": "-----xyz-----",
    "access_token": -------xyz------"
}


Step 3 : How to call Rest API from Postman or other rest client
Insert / update

Method:POST
URL:https://ap2.salesforce.com/services/apexrest/webservices
 (<instance_url>/services/apexrest/< urlMapping of class>)
Header1:Authorization
Value1:Bearer <space> <access_token>
Header2:Content-Type
Value2:application/json




Now select raw and write json in it like:

For insert contact
{
    "JSONString": {
        "attributes": {
            "type": "Contact"
        },
        "FirstName": "John",
        "LastName": "test",
        "Email": " testing @gmail.com",
        "Phone": "12121212"
    }
}

For update contact:
{
    "JSONString": {
        "attributes": {
            "type": "Contact"
        },
        "id":"00328000003KfhB"
        "FirstName": "John",
        "LastName": "testing",
        "Email": "testing@gmail.com",
        "Phone": "12121212"
    }
}