Saturday, 24 November 2018

Rest Integration With Postman



Step: 1

  • For Rest Integration with using POSTMAN tool first you need to create the Connected app in your org.like shown in the below Image.





Step :2


  • After saving the connected app you will be getting a Consumer key And Consumer Secret.





Step:3

  • Now you need to install/ add postman chrome extension and register with your Google account.
  • Then you need to copy Callback URL From connected app then paste the URL in postman url section as shown in below pic.
  •  we need to write body section under Grant type
  • syntax: grant_type=password&client_id=your_clientid_here&client_secret=your_client_secret_here&username=your_username&password=your_password_hereWithsecuritytoken 
  • Client_id = Consumer Key, client_secret = Consumer secret,password = your password with securitytoken 
  • Ex:  grant_type=password&client_id=3MVG9YDQS5WtC11rG9SGkhKCagCn2tK1zutzAjxjg0FiN9jt97YDsyW27HbUS2gjxWClYumtI7DrKYmZ5MFVy &client_secret=4464276243039650923&username=malleshmyakala@developer.com&password=Mallesh@25s2TZJjF8P9zLx1RAwTtNYNABD
  • URL type should be POST, Then click on send.






  • After hitting the send button you will be seeing the authentication code.
  • when status code =  "200 ok" it means executed successfully 
  • here we will get Access Token                                                                                 authentication code:                                                                                                                   {                                                                                                                                           "access_token":"00D6F000001O0Y4!ARAAQLbZ0_vR.ijv3sdsKAu6d5xF7Aj2Mmt2_nXQDNf1aPgfICSrIynLsqdlebVRY0de0bZTxspSFmhXYv0o6ftnzAIn8Mpd",                "instance_url": "https://manuyadav-dev-ed.my.salesforce.com",                                              "id": "https://login.salesforce.com/id/00D6F000001O0Y4UAK/0056F000006j2ohQAA", "token_type": "Bearer",                                                                                                 "issued_at": "1542617533665",                                                                                   "signature": "k8yPqZ9WRAzBGAiVLdsBrnmz8ZRN44wi26m5tlZl0BA="                              }
  • Now we need to setup Content Type = "application/json" and Authentication= "Bearer access_token" in Headers Tab.                                                                                         Ex:Bearer 00D6F000001O0Y4!ARAAQLbZ0_vR.ijv3sdsKAu6d5xF7Aj2Mmt2_nXQDNf1aPgfICSrIynLsqdlebVRY0de0bZTxspSFmhXYv0o6ftnzAIn8Mpd


Step:4

 write the code for Rest API like below.

@RestResource(urlMapping='/AccountService/*')
Global class RestServiceTest {
    
    
    @httpPost
    global static string dopost(string accName, string Phone){
        Account acc = new Account();
      acc.Name = accName;
        acc.Phone=Phone;
        insert acc;
        
        return'Post method called successfully'+Acc.Id; 
    }
    
       @httpGet
    global static List<account> doGet(){
        
        RestRequest request = RestContext.request;
        
        string AccId=request.requestURI.substring(request.requestURI.lastindexOf('/')+1);
        system.debug('====accId===='+accId);
        list<account> lstAccs= [select id,name,phone from account where id=:accId];
        return lstAccs;
        
        }
    }

After writing the Code for Rest API then we need to create URL.

EX: https://manuyadav-dev-ed.my.salesforce.com/services/apexrest/AccountService
  • https://manuyadav-dev-ed.my.salesforce.com/(This is your org URL)
  • services/apexrest/(this is common service for postman)
  • AccountService(this is urlMapping name in your Rest class @RestResource)      


enter  (Postman URL)  in post section in postman tool. write the JSON code in the body section. then hit send button.
.
If Status is "200 ok" then you will find  created account Id. as shown in the below pic
this is the process for creating the record using REST API service through POSTMAN tool.



  • Now coming to GET process 
  • Under the get type enter the URL
  • EX: https://manuyadav-dev-ed.my.salesforce.com/services/apexrest/0016F00002zmISH                     https://manuyadav-dev-ed.my.salesforce.com (Your Org URL )                                                       /services/apexrest/   (Common service)                                                                                            0016F00002zmISH (This is account ID)
  • Then just hit send button now you will be getting the particular account details as shown in below pic.




This is the process for getting the record details using REST API Service through Postman.



No comments:

Post a Comment