kept getting http400 error when i try to send this Rest message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2024 10:49 PM
need some help as i cannot figure out why kept getting http 400 error . i am using oauth2 authentication
// Initial log to ensure script execution
gs.log('UI Action script started', 'DellSOAPRequest');
// Function to retrieve the sys_id of the OAuth profile
function getOAuthProfileSysId(profileName) {
var gr = new GlideRecord('oauth_entity_profile');
gr.addQuery('name', profileName);
gr.query();
if (gr.next()) {
return gr.sys_id.toString();
} else {
throw new Error('OAuth profile not found: ' + profileName);
}
}
try {
// Retrieve the sys_id of the Dell default_profile
var oauthProfileName = 'Dell default_profile';
var oauthProfileSysId = getOAuthProfileSysId(oauthProfileName);
gs.log('Retrieved OAuth profile sys_id: ' + oauthProfileSysId, 'DellSOAPRequest');
// Get current user's information
var currentUser = gs.getUser();
var firstName = currentUser.getFirstName();
var lastName = currentUser.getLastName();
var emailAddress = currentUser.getEmail();
var phoneNumber = currentUser.getPhone();
var companyName = currentUser.getCompanyID();
gs.log('Current User Info - FirstName: ' + firstName + ', LastName: ' + lastName + ', Email: ' + emailAddress + ', Phone: ' + phoneNumber + ', Company: ' + companyName, 'DellSOAPRequest');
// Initialize the RESTMessageV2 object
var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://apigtwb2cnp.us.dell.com/Sandbox/support/case/v3/WebCase');
r.setHttpMethod('POST');
// Set OAuth 2.0 authentication profile using sys_id
r.setAuthenticationProfile('oauth2', oauthProfileSysId);
// Set HTTP headers
r.setRequestHeader('SOAPAction', 'http://ph.services.dell.com/Server/AlertOperation');
r.setRequestHeader('Content-Type', 'text/xml');
gs.log('HTTP headers set', 'DellSOAPRequest');
// Set request body with XML content
var xmlPayload = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://ph.services.dell.com/Server/">' +
'<soapenv:Header />' +
'<soapenv:Body>' +
'<ser:AlertRequest>' +
'<SourceHeader>' +
'<ClientId>730634</ClientId>' +
'<ClientType>HELPDESK</ClientType>' +
'<ClientHostName></ClientHostName>' +
'<ClientIPAddress>0.0.0.0</ClientIPAddress>' +
'<GuId></GuId>' +
'<ClientVersion></ClientVersion>' +
'<RequestId></RequestId>' +
'</SourceHeader>' +
'<CustomerHeader>' +
'<CompanyName>' + companyName + '</CompanyName>' +
'<CountryCodeISO></CountryCodeISO>' +
'<EmailOptIn></EmailOptIn>' +
'<PrimaryContact>' +
'<FirstName>' + firstName + '</FirstName>' +
'<LastName>' + lastName + '</LastName>' +
'<Country></Country>' +
'<TimeZone></TimeZone>' +
'<PhoneNumber1>' + phoneNumber + '</PhoneNumber1>' +
'<EmailAddress>' + emailAddress + '</EmailAddress>' +
'<PreferContactMethod>email</PreferContactMethod>' +
'<PreferContactTimeframe></PreferContactTimeframe>' +
'<PreferLanguage>en</PreferContactTimeframe>' +
'</PrimaryContact>' +
'</CustomerHeader>' +
'<AlertData>' +
'<EventId>2</EventId>' +
'<TrapId>0</TrapId>' +
'<EventSource>Server</EventSource>' +
'<Severity>3</Severity>' +
'<Message></Message>' +
'<Timestamp>2024-05-03T13:30:00+0200</Timestamp>' +
'<ServiceTag>APITG05</ServiceTag>' +
'<PartSerialNo></PartSerialNo>' +
'<FileToken></FileToken>' +
'<AgentSource></AgentSource>' +
'<DeviceName></DeviceName>' +
'<DeviceIP></DeviceIP>' +
'<DeviceModel></DeviceModel>' +
'<DeviceType></DeviceType>' +
'<OS></OS>' +
'<DiagnosticsOptIn></DiagnosticsOptIn>' +
'<CaseSeverity>Medium</CaseSeverity>' +
'<Code></Code>' +
'<RecommendationId></RecommendationId>' +
'</AlertData>' +
'<WebCaseOperation>' +
'<Operation>ALERTS</Operation>' +
'</WebCaseOperation>' +
'</ser:AlertRequest>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
gs.log('XML Payload: ' + xmlPayload, 'DellSOAPRequest');
r.setRequestBody(xmlPayload);
gs.log('Request body set', 'DellSOAPRequest');
// Execute the REST message
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
// Log the response for debugging
gs.log('Response Body: ' + responseBody, 'DellSOAPRequest');
gs.log('HTTP Status: ' + httpStatus, 'DellSOAPRequest');
} catch(ex) {
// Handle any exceptions that occur during the execution
var message = ex.message;
gs.log('Error: ' + message, 'DellSOAPRequest');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2024 03:22 PM - edited 06-16-2024 03:23 PM
Hi @chercm ,
I think the issue can be either of these places-
Incorrect OAuth configuration
Incorrect endpoint URL
Invalid or missing headers
Missing required fields
Incorrect encoded data
Also please use the below script to identify the root cause-
// Initial log to ensure script execution
gs.log('UI Action script started', 'DellSOAPRequest');
// Function to retrieve the sys_id of the OAuth profile
function getOAuthProfileSysId(profileName) {
var gr = new GlideRecord('oauth_entity_profile');
gr.addQuery('name', profileName);
gr.query();
if (gr.next()) {
return gr.sys_id.toString();
} else {
throw new Error('OAuth profile not found: ' + profileName);
}
}
try {
// Retrieve the sys_id of the Dell default_profile
var oauthProfileName = 'Dell default_profile';
var oauthProfileSysId = getOAuthProfileSysId(oauthProfileName);
gs.log('Retrieved OAuth profile sys_id: ' + oauthProfileSysId, 'DellSOAPRequest');
// Get current user's information
var currentUser = gs.getUser();
var firstName = currentUser.getFirstName();
var lastName = currentUser.getLastName();
var emailAddress = currentUser.getEmail();
var phoneNumber = currentUser.getPhone();
var companyName = currentUser.getCompanyID();
gs.log('Current User Info - FirstName: ' + firstName + ', LastName: ' + lastName + ', Email: ' + emailAddress + ', Phone: ' + phoneNumber + ', Company: ' + companyName, 'DellSOAPRequest');
// Initialize the RESTMessageV2 object
var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://apigtwb2cnp.us.dell.com/Sandbox/support/case/v3/WebCase');
r.setHttpMethod('POST');
// Set OAuth 2.0 authentication profile using sys_id
r.setAuthenticationProfile('oauth2', oauthProfileSysId);
// Set HTTP headers
r.setRequestHeader('SOAPAction', 'http://ph.services.dell.com/Server/AlertOperation');
r.setRequestHeader('Content-Type', 'text/xml');
gs.log('HTTP headers set', 'DellSOAPRequest');
// Set request body with XML content
var xmlPayload = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://ph.services.dell.com/Server/">' +
'<soapenv:Header />' +
'<soapenv:Body>' +
'<ser:AlertRequest>' +
'<SourceHeader>' +
'<ClientId>730634</ClientId>' +
'<ClientType>HELPDESK</ClientType>' +
'<ClientHostName></ClientHostName>' +
'<ClientIPAddress>0.0.0.0</ClientIPAddress>' +
'<GuId></GuId>' +
'<ClientVersion></ClientVersion>' +
'<RequestId></RequestId>' +
'</SourceHeader>' +
'<CustomerHeader>' +
'<CompanyName>' + companyName + '</CompanyName>' +
'<CountryCodeISO></CountryCodeISO>' +
'<EmailOptIn></EmailOptIn>' +
'<PrimaryContact>' +
'<FirstName>' + firstName + '</FirstName>' +
'<LastName>' + lastName + '</LastName>' +
'<Country></Country>' +
'<TimeZone></TimeZone>' +
'<PhoneNumber1>' + phoneNumber + '</PhoneNumber1>' +
'<EmailAddress>' + emailAddress + '</EmailAddress>' +
'<PreferContactMethod>email</PreferContactMethod>' +
'<PreferContactTimeframe></PreferContactTimeframe>' +
'<PreferLanguage>en</PreferContactTimeframe>' +
'</PrimaryContact>' +
'</CustomerHeader>' +
'<AlertData>' +
'<EventId>2</EventId>' +
'<TrapId>0</TrapId>' +
'<EventSource>Server</EventSource>' +
'<Severity>3</Severity>' +
'<Message></Message>' +
'<Timestamp>2024-05-03T13:30:00+0200</Timestamp>' +
'<ServiceTag>APITG05</ServiceTag>' +
'<PartSerialNo></PartSerialNo>' +
'<FileToken></FileToken>' +
'<AgentSource></AgentSource>' +
'<DeviceName></DeviceName>' +
'<DeviceIP></DeviceIP>' +
'<DeviceModel></DeviceModel>' +
'<DeviceType></DeviceType>' +
'<OS></OS>' +
'<DiagnosticsOptIn></DiagnosticsOptIn>' +
'<CaseSeverity>Medium</CaseSeverity>' +
'<Code></Code>' +
'<RecommendationId></RecommendationId>' +
'</AlertData>' +
'<WebCaseOperation>' +
'<Operation>ALERTS</Operation>' +
'</WebCaseOperation>' +
'</ser:AlertRequest>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
gs.log('XML Payload: ' + xmlPayload, 'DellSOAPRequest');
r.setRequestBody(xmlPayload);
gs.log('Request body set', 'DellSOAPRequest');
// Execute the REST message
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
// Log the response for debugging
gs.log('Response Body: ' + responseBody, 'DellSOAPRequest');
gs.log('HTTP Status: ' + httpStatus, 'DellSOAPRequest');
if (httpStatus != 200) {
gs.log('Error: ' + response.getErrorMessage(), 'DellSOAPRequest');
}
} catch(ex) {
// Handle any exceptions that occur during the execution
var message = ex.message;
gs.log('Error: ' + message, 'DellSOAPRequest');
}
Use the above script to identify the cause of exact issue. Check the logs.
Try to test the method and share the response here. you can configure similar thing to test it with postman to find the issue.
If my response has resolved your query, please consider giving it a thumbs up and marking it as the correct answer!
Thanks & Regards,
Sanjay Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2024 06:52 PM
@Community Alums if this is should be the request header ,
Request Headers:
Authorization: Bearer {Token}
Content-Type: text/xml
Request Body: input the SOAP request xml
Response Format: XML
how can i get hold of the bearer {token} ?