You need to sign in to do that
Don't have an account?
Michael M
502 error on callout
Hello, I'm trying to make a REST API callout, with the use of named credentials. However when I try to make the callout, I get a 502 error. Am I missing anything basic (this is my first callout :)
public class EMedCalloutsExtension {
public final Lead referral;
public String requestLabel;
public String result {get;set;}
public List<Object> emedData {get;set;}
Public EMedCalloutsExtension(ApexPages.StandardController stdController){
this.referral = (Lead)stdController.getRecord();
}
public Object makePostCallout() {
Lead ref =[Select Id, firstname, lastname, gender__c, patient_dob__c, patient_ssn__c from Lead where Id = :ApexPages.currentPage().getParameters().get('id')];
system.debug('***NAME OF REFERRAL***: '+ ref.firstname + ' ' + ref.lastname);
String ssnReplace;
if (ref.Patient_SSN__c != null){
ssnReplace = ref.Patient_SSN__c.replace('-','');
}
String genderLetter;
if (ref.Gender__c == 'Male'){
genderLetter = 'M';
} else if (ref.Gender__c == 'Female'){
genderLetter = 'F';
}
String first;
String second;
String third;
String d = String.valueOf(ref.Patient_DOB__c);
system.debug('***dob string raw: ' + d);
first = d.substring(0,4);
second = d.substring(5,7);
third = d.substring(8,10);
String dob = first + second + third;
system.debug('***dob formatted: ' + dob);
Map<string, string> patientInfoMap = new Map<string, string>();
patientInfoMap.put('social_security_number', ssnReplace);
patientInfoMap.put('birthdate', dob);
patientInfoMap.put('gender', genderLetter);
patientInfoMap.put('last_name', ref.lastname);
patientInfoMap.put('first_name', ref.firstname);
List<Object> patientInfoList = new List<Object>();
patientInfoList.add(patientInfoMap);
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('processing_mode', 'Realtime');
gen.writeStringField('request_type', '270');
gen.writeStringField('information_source_id', '1');
gen.writeStringField('submitter_system_id', '2');
gen.writeStringField('submitter_system_type', 'api');
gen.writeStringField('submitter_request_id', 'R1');
gen.writeStringField('submitter_user_id', '3');
gen.writeObjectField('subscribers', patientInfoList);
gen.writeEndObject();
String jsonS = gen.getAsString();
System.debug('***BODY: '+jsonS);
// Continuation con = new Continuation(40);
//con.continuationMethod='processResponse';
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Emed/');
req.setHeader('ContentType', 'application/json');
req.setHeader('Accept', 'application/json');
System.debug('***HEADER: ' + req.getHeader('Authorization'));
req.setMethod('POST');
req.setBody(jsonS);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug('Request: ' + req);
//print the header
//get the list of header names (keys)
string[] headerkeys2 = res.getHeaderKeys();
//create an object to store your header key-value pairs
Map<string, string> headers2 = new map<string, string>();
//iterate through they keys, and populate your map
for(string s : headerkeys2){
headers2.put(s,res.getHeader(s));
system.debug('header of response: ' + s + ' value: ' + res.getHeader(s));
}
String result = null;
Integer statusCode = res.getStatusCode();
system.debug('***statusCode is***: ' + statusCode);
if(statusCode == 200){
system.debug('***API invoked successfully***');
result = res.getBody();
system.debug('***RESULT***: ' + result);
}
return result;
}
public Object processResponse() {
HttpResponse response = Continuation.getResponse(this.requestLabel);
Integer statusCode = response.getStatusCode();
system.debug('***statusCode is ***:' + statusCode);
// Set the result variable that is displayed on the Visualforce page
if (statusCode == 200) {
system.debug('statusCode is ' + statusCode);
this.result = response.getBody();
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
emedData = (List<Object>) results.get('emedData');
}
return null;
}
}
public class EMedCalloutsExtension {
public final Lead referral;
public String requestLabel;
public String result {get;set;}
public List<Object> emedData {get;set;}
Public EMedCalloutsExtension(ApexPages.StandardController stdController){
this.referral = (Lead)stdController.getRecord();
}
public Object makePostCallout() {
Lead ref =[Select Id, firstname, lastname, gender__c, patient_dob__c, patient_ssn__c from Lead where Id = :ApexPages.currentPage().getParameters().get('id')];
system.debug('***NAME OF REFERRAL***: '+ ref.firstname + ' ' + ref.lastname);
String ssnReplace;
if (ref.Patient_SSN__c != null){
ssnReplace = ref.Patient_SSN__c.replace('-','');
}
String genderLetter;
if (ref.Gender__c == 'Male'){
genderLetter = 'M';
} else if (ref.Gender__c == 'Female'){
genderLetter = 'F';
}
String first;
String second;
String third;
String d = String.valueOf(ref.Patient_DOB__c);
system.debug('***dob string raw: ' + d);
first = d.substring(0,4);
second = d.substring(5,7);
third = d.substring(8,10);
String dob = first + second + third;
system.debug('***dob formatted: ' + dob);
Map<string, string> patientInfoMap = new Map<string, string>();
patientInfoMap.put('social_security_number', ssnReplace);
patientInfoMap.put('birthdate', dob);
patientInfoMap.put('gender', genderLetter);
patientInfoMap.put('last_name', ref.lastname);
patientInfoMap.put('first_name', ref.firstname);
List<Object> patientInfoList = new List<Object>();
patientInfoList.add(patientInfoMap);
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('processing_mode', 'Realtime');
gen.writeStringField('request_type', '270');
gen.writeStringField('information_source_id', '1');
gen.writeStringField('submitter_system_id', '2');
gen.writeStringField('submitter_system_type', 'api');
gen.writeStringField('submitter_request_id', 'R1');
gen.writeStringField('submitter_user_id', '3');
gen.writeObjectField('subscribers', patientInfoList);
gen.writeEndObject();
String jsonS = gen.getAsString();
System.debug('***BODY: '+jsonS);
// Continuation con = new Continuation(40);
//con.continuationMethod='processResponse';
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Emed/');
req.setHeader('ContentType', 'application/json');
req.setHeader('Accept', 'application/json');
System.debug('***HEADER: ' + req.getHeader('Authorization'));
req.setMethod('POST');
req.setBody(jsonS);
Http http = new Http();
HTTPResponse res = http.send(req);
system.debug('Request: ' + req);
//print the header
//get the list of header names (keys)
string[] headerkeys2 = res.getHeaderKeys();
//create an object to store your header key-value pairs
Map<string, string> headers2 = new map<string, string>();
//iterate through they keys, and populate your map
for(string s : headerkeys2){
headers2.put(s,res.getHeader(s));
system.debug('header of response: ' + s + ' value: ' + res.getHeader(s));
}
String result = null;
Integer statusCode = res.getStatusCode();
system.debug('***statusCode is***: ' + statusCode);
if(statusCode == 200){
system.debug('***API invoked successfully***');
result = res.getBody();
system.debug('***RESULT***: ' + result);
}
return result;
}
public Object processResponse() {
HttpResponse response = Continuation.getResponse(this.requestLabel);
Integer statusCode = response.getStatusCode();
system.debug('***statusCode is ***:' + statusCode);
// Set the result variable that is displayed on the Visualforce page
if (statusCode == 200) {
system.debug('statusCode is ' + statusCode);
this.result = response.getBody();
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
emedData = (List<Object>) results.get('emedData');
}
return null;
}
}
Is the endpoint working fine? Can you access it through POSTMAN ? You get 502 when problems are occurring at the server-side of a connection
Thank you for your response! Yes, when I access it on POSTMAN it is working fine. I made a change on the SF side, and now there is a different error. Now I am getting a 403 error code.