function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

How to Write Test Class for HTTP POST Method

Hi Everyone,
How to write
 public static String authMethod(){
            Pardot_Credential_Setting__c objSetting = customSetting();
            
            if(objSetting != null){
                String email = objSetting.Pardot_Email__c;
                String password = objSetting.Pardot_Password__c;
                String userKey = objSetting.User_Api_Key__c;

                HttpRequest req = new HttpRequest();
                req.setEndpoint( objSetting.Pardot_BaseUrl__c+'/api/login/version/4' );
                req.setMethod( 'POST' );
                req.setBody( 'email=' + email + '&password=' + password + '&user_key=' + userKey );

                HttpResponse res = new Http().send( req );

                System.debug('>>>>>>>>res'+ res );
                System.debug('>>>>>>>>>res.getStatus()'+ res.getStatus() );
                System.debug('>>>>>>res.getBody()'+ res.getBody() );

                String response = res.getBody();
                
                Integer startIdx;
                Integer endIdx;
                String apiKey;
                if(response.contains('<api_key>')){
                    startIdx = response.indexOf( '<api_key>' ) + 9;
                    endIdx = response.indexOf( '</api_key>' );
                    apiKey = response.substring( startIdx, endIdx );
                    System.debug( '>>>>>>>apiKey'+ apiKey );
                    return apiKey;
                }
            }
            
            return null;
        }
ShirishaShirisha (Salesforce Developers) 

Hi Bablu,

Greetings!

Can you please check the sample code to cover the httppost request as below:

https://developer.salesforce.com/forums/?id=9060G000000XdDoQAK

https://developer.salesforce.com/forums/?id=906F0000000MLciIAG

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

SSP AdminSSP Admin
Hi Bablu Kumar,
It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com

Thanks,
Jarvis SFDC team