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
Zachary Alexander 30Zachary Alexander 30 

Callout -- 302 Found -- Vendor says its a problem with our network.

To the Salesforce Gods,

I beseech thee for thine aid, oh Beneficent Dieties who doth inhabit this Wondrous Realm. Forsooth... *ahem*... I'm trying to make a POST callout to a vendor's website from our Salesforce instance. In all cases, I am getting back 302 Found. Normally, this just requires catching the return URL redirect and resubmitting the POST, but that is not working in this case. The vendor says that their site is set to always redirect to a home page rather than giving an error.

After working with one of the vendor's technicians, he has confirmed that the header / body of the request from Salesforce is correct (I'll include some code below). Please note: the code shown below does not contain the 302 Found redirect loop -- the loop always fails after hitting the redirect page so I've simplified the code below by ommitting it.

Has anyone else encountered an error like this? Is it the vendor's fault somehow? They're sandbox is chronically down. Maybe they're blocking access?

Or... is there something obviously wrong with the code?

------------CODE-------------

public class CatalystPatientInsert {
   
    public static HttpResponse makePostCallout() {
       
        //Help for this class found at "https://salesforce.stackexchange.com/questions/11015/basic-http-authentication"
        String username = 'HiddenUserName;
        String password = 'HiddenPassword;
       
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String url = 'https://sandbox.vendor.com/api/students/';
        request.setEndpoint(url);
        request.setMethod('POST');
        Blob headerValue = Blob.valueOf(username +':'+password);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        system.debug(authorizationHeader);
        request.setHeader('Authentication', authorizationHeader);
        request.setHeader('User-Agent','CompanyName');
        request.setHeader('Content-Type', 'application/json');
       
        //Help for this class found at "https://developer.salesforce.com/forums/?id=906F0000000BQ30IAG"
        JsonHelpWrapper jwrap = new JsonHelpWrapper('ThapistId1', 'TherapistId2', 'JJ',
                               'Test2', null, '2014-03-26T10:05:44', '2014-03-26T10:05:44',
                               'm', true, '2014-03-26T10:05:44', null,
                               'test, jj', 'Diagnosis X', false,
                               'America/New_York');
       
        String requestValue = JSON.serialize(jwrap);
        System.debug(requestValue);
        request.setBody(requestValue);
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
            System.debug(response.getHeader('Location'));

        } else {
            System.debug(response.getBody());
        }
        return response;
       
    }
   
}
Best Answer chosen by Zachary Alexander 30
Zachary Alexander 30Zachary Alexander 30
No help from this website so I instead contacted a consulting company to look at my code and deebug logs. The code seems to be correct according to them: the problem is likely on the Vendor's side.

In case anyone is interested, I've included the most recent version of code (not finalized version, of course) below:

--------------------------

public class CatalystPatientInsert {
    
    public static HttpResponse makePostCallout() {
        
        //Help for this class found at "https://salesforce.stackexchange.com/questions/11015/basic-http-authentication"
        String username = 'USERNAME';
        String password = 'PASSWORD';
        Boolean redirect = false;
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String url = 'https://sandbox.vendor.com/api/students/';
        request.setEndpoint(url);
        request.setMethod('POST');
        Blob headerValue = Blob.valueOf(username +':'+password);
        String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
        system.debug(authorizationHeader);
        request.setHeader('User-Agent','Hopebridge');
        request.setHeader('Content-Type', 'application/json;charset-UTF-8');
        request.setHeader('Authentication', authorizationHeader);
        
        //Help for this class found at "https://developer.salesforce.com/forums/?id=906F0000000BQ30IAG"
        JSONHelpWrapper jwrap = new JSONHelpWrapper('7FBBDC6A-8206-442D-B06D-5724C40CE46B', '7FBBDC6A-8206-442D-B06D-5724C40CE46B', 'JJ',
                               'Test2', null, '2014-03-26T10:05:44', '2014-03-26T10:05:44',
                               'm', true, '2014-03-26T10:05:44', null,
                               'test, jj', 'Autism Spectrum Disorder', false,
                               'America/New_York');
        
        String requestValue = JSON.serialize(jwrap);
        System.debug(requestValue);
        request.setBody(requestValue);
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() >= 300 && response.getStatusCode() <= 307 && response.getStatusCode() != 306) {
            do {
                redirect = false; // reset the value each time
                String loc = response.getHeader('Location'); // get location of the redirect
                System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
                System.debug(response.getHeader('Location'));
                if(loc == null) {
                    redirect = false;
                    continue;
                }
                request.setBody(requestValue);
                request = new HttpRequest();
                request.setEndpoint(loc);
                request.setMethod('POST');
                request.setBody(requestValue);
                response = http.send(request);
                if(response.getStatusCode() != 500) {
                    if(response.getStatusCode() >= 300 && response.getStatusCode() <= 307 && response.getStatusCode() != 306) {
                        System.debug(response.getHeader('Location'));
                        redirect = true;
                    }
                }
                
                
            } while (redirect && Limits.getCallouts() != Limits.getLimitCallouts());
            
        }
        
        if (response.getStatusCode() != 201) {
            
            System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
            System.debug(response.getHeader('Location'));
            
        }
        
        System.debug(response.getBody());
        return response;
        
    }
    
}

---------------------------

public class JSONHelpWrapper {
    
    Public String BCBA {get;set;}
    Public String LeadTherapistId {get;set;}
    Public String FirstName {get;set;}
    Public String LastName {get;set;}
    Public String SiteId {get;set;}
    Public String DateOfBirth {get;set;}
    Public String EntryDate {get;set;}
    Public String Gender {get;set;}
    Public Boolean Active {get;set;}
    Public String DateLastUpdated {get;set;}
    Public String ExternalId {get;set;}
    Public String StudentCode {get;set;}
    Public String Diagnosis {get;set;}
    Public Boolean IsUsingTeachingSessions {get;set;}
    Public String TimeZone {get;set;}
    
    Public CatalystWrapper(String BCBA, String LeadTherapistId, String FirstName,
                               String LastName, String SiteId, String DateOfBirth, String EntryDate,
                               String Gender, Boolean Active, String DateLastUpdated, String ExternalId,
                               String StudentCode, String Diagnosis, Boolean IsUsingTeachingSessions,
                               String TimeZone) {
                                   This.BCBA = '7FBBDC6A-8206-442D-B06D-5724C40CE46B';
                                   This.LeadTherapistId = '7FBBDC6A-8206-442D-B06D-5724C40CE46B';
                                   This.FirstName = 'JJ';
                                   This.LastName = 'Test2';
                                   This.SiteId = null;
                                   This.DateOfBirth = '2014-03-26T10:05:44';
                                   This.EntryDate = '2014-03-26T10:05:44';
                                   This.Gender = 'm';
                                   This.Active = true;
                                   This.DateLastUpdated = '2014-03-26T10:05:44';
                                   This.ExternalId = null;
                                   This.StudentCode = 'test, jj';
                                   This.Diagnosis = 'Autism Spectrum Disorder';
                                   This.IsUsingTeachingSessions = false;
                                   This.TimeZone = 'America/New_York';
                               }
    
}