+ Start a Discussion
Sunny NarulaSunny Narula 

Webservice callout with Token and Json string in POST request

I have two callout to some external web service [both are of type POST]
a) First one returns Token  (that is to be used in second callout)
I m able to call and get the token in Apex.

b) Second one creates a USER (I need to pass few details to this as to create user) 
Please guide me how to pass header with token and json string in Apex to create user.

Basically I have following insctructions, but I m not getting to how to use that in Apex to work properly 

Create user via POST to https://abcdef.com/api/tcUser

headers:
  Content-Type: application/json
  Authorization: Bearer {{token recieved in step 1}}

body example (json):
 {
      "Name": "USER_1314",
      "UserCode": "USER_1314",
      "UserState": true,
      "AllowApiLogin": true,
      "ShowHeaderToolTip": true,
      "CurrencyId": "e708dffd-ce3a-4c4e-a98a-48b67824c7f7",
      "LanguageId": "f57a2f60-233d-400f-9607-33c55bdff4fc",
      "UnitDistanceId": "30857154-f8e4-4e7b-86b5-ad9b5cd05835",
      "UnitPressureId": "1115140c-ddf9-4cfb-af4c-38fd21ddadc2",
      "UnitTemperatureId": "a61cfca0-ea07-49b4-a937-11c58b7cb24a",
      "UnitTreadDepthId": "b71e84a0-8702-4e3e-a421-daa9051f0d0c",
      "RoleId": "1a0b66be-ff35-4ca4-931f-00003985e261",
      "ContactEmail": "test@test_zoom.com"
}

 

 

Best Answer chosen by Sunny Narula
Shruti SShruti S
Here is how you can do this - 
  1. Create a separate class that will hold the User Information like as shown below - 
    public class UserInformation {
        public String Name;
        public String UserCode;
        public Boolean UserState;
        public Boolean AllowApiLogin;
        public Boolean ShowHeaderToolTip;
        public String CurrencyId;
        public String LanguageId;
        public String UnitDistanceId;
        public String UnitPressureId;
        public String UnitTemperatureId;
        public String UnitTreadDepthId;
        public String RoleId;
        public String ContactEmail;
    }
  2. Then create an another Apex Class called API which will have a method that will call the API Endpoint as shown below - 
    public class API {
        private static UserInformation generateUserInfo() {
            UserInformation usrInfo = new UserInformation();
            usrInfo.Name                = 'USER_1314';
            usrInfo.UserCode            = 'USER_1314';
            usrInfo.UserState           = TRUE;
            usrInfo.AllowApiLogin       = TRUE;
            usrInfo.ShowHeaderToolTip   = TRUE;
            usrInfo.CurrencyId          = 'e708dffd-ce3a-4c4e-a98a-48b67824c7f7';
            usrInfo.LanguageId          = 'f57a2f60-233d-400f-9607-33c55bdff4fc';
            usrInfo.UnitDistanceId      = '30857154-f8e4-4e7b-86b5-ad9b5cd05835';
            usrInfo.UnitPressureId      = '1115140c-ddf9-4cfb-af4c-38fd21ddadc2';
            usrInfo.UnitTemperatureId   = 'a61cfca0-ea07-49b4-a937-11c58b7cb24a';
            usrInfo.UnitTreadDepthId    = 'b71e84a0-8702-4e3e-a421-daa9051f0d0c';
            usrInfo.RoleId              = '1a0b66be-ff35-4ca4-931f-00003985e261';
            usrInfo.ContactEmail        = 'test@test_zoom.com';
            
            return usrInfo;
        }
        
        public static void createUser( String token ) {
            Http http = new Http();
            HttpRequest httpReq = new  HttpRequest();
            
            httpReq.setEndpoint( 'https://abcdef.com/api/tcUser' );
            httpReq.setMethod( 'POST' );
            httpReq.setHeader( 'Content-Type', 'application/json' );
            httpReq.setHeader( 'Accept', 'application/json' );
            httpReq.setHeader( 'Authorization', 'Bearer ' + token );
            
            String body = JSON.serialize( generateUserInfo() );
            
            httpReq.setBody( body );
            
            HttpResponse httpRes = http.send( httpReq );
        }
    }
    I hope this makes sense. Let me know if you have any other doubts.

All Answers

Shruti SShruti S
Here is how you can do this - 
  1. Create a separate class that will hold the User Information like as shown below - 
    public class UserInformation {
        public String Name;
        public String UserCode;
        public Boolean UserState;
        public Boolean AllowApiLogin;
        public Boolean ShowHeaderToolTip;
        public String CurrencyId;
        public String LanguageId;
        public String UnitDistanceId;
        public String UnitPressureId;
        public String UnitTemperatureId;
        public String UnitTreadDepthId;
        public String RoleId;
        public String ContactEmail;
    }
  2. Then create an another Apex Class called API which will have a method that will call the API Endpoint as shown below - 
    public class API {
        private static UserInformation generateUserInfo() {
            UserInformation usrInfo = new UserInformation();
            usrInfo.Name                = 'USER_1314';
            usrInfo.UserCode            = 'USER_1314';
            usrInfo.UserState           = TRUE;
            usrInfo.AllowApiLogin       = TRUE;
            usrInfo.ShowHeaderToolTip   = TRUE;
            usrInfo.CurrencyId          = 'e708dffd-ce3a-4c4e-a98a-48b67824c7f7';
            usrInfo.LanguageId          = 'f57a2f60-233d-400f-9607-33c55bdff4fc';
            usrInfo.UnitDistanceId      = '30857154-f8e4-4e7b-86b5-ad9b5cd05835';
            usrInfo.UnitPressureId      = '1115140c-ddf9-4cfb-af4c-38fd21ddadc2';
            usrInfo.UnitTemperatureId   = 'a61cfca0-ea07-49b4-a937-11c58b7cb24a';
            usrInfo.UnitTreadDepthId    = 'b71e84a0-8702-4e3e-a421-daa9051f0d0c';
            usrInfo.RoleId              = '1a0b66be-ff35-4ca4-931f-00003985e261';
            usrInfo.ContactEmail        = 'test@test_zoom.com';
            
            return usrInfo;
        }
        
        public static void createUser( String token ) {
            Http http = new Http();
            HttpRequest httpReq = new  HttpRequest();
            
            httpReq.setEndpoint( 'https://abcdef.com/api/tcUser' );
            httpReq.setMethod( 'POST' );
            httpReq.setHeader( 'Content-Type', 'application/json' );
            httpReq.setHeader( 'Accept', 'application/json' );
            httpReq.setHeader( 'Authorization', 'Bearer ' + token );
            
            String body = JSON.serialize( generateUserInfo() );
            
            httpReq.setBody( body );
            
            HttpResponse httpRes = http.send( httpReq );
        }
    }
    I hope this makes sense. Let me know if you have any other doubts.
This was selected as the best answer
Sunny NarulaSunny Narula

Hi Shruti 

Can you please suggest me a way to hold the token for few days.
As I have two API's to call.
First one returns the token and the another one created the User (for creating user, valid token in mandatory)

The token returned in the first API is valid for 11 days. Therefore, if I can check it expiry date before calling everytime.
Where can I store this token for say 11 days and then if the token is expired then call for the new one...
(do I have to create a new object to told this token, is this the only approach, please suggest.)

Thanks in advance