
When you use the ajax.call function to communicate with a web service, you receive a status from the server such as:
Sometimes, it seems that the call does not work and you receive a generic error message. This can be:
It can be difficult to know where the issue comes from.
Web services can combine many different settings:
You use Client certificates to authenticate on a server. Sometimes you cannot connect to a server with a certificate.
You can use the option ignoreClientCertificate to avoid the certificate authentication.
ctx.ajax.call({
url: url,
method: 'GET',
contentType: "application/json",
headers: headers,
ignoreClientCertificate: true,
success: function(res, status, xhr) {
rootData.xCSRF = xhr.headers["x-csrf-token"];
var value = xhr.headers["Set-Cookie"];
rootData.Cookies = value.split('; ')[0] + "; ";
sc.endStep();
},
error: function(xhr, status, statusText) {
ctx.log('error get')
sc.endStep();
}
});
API may need such certificates. They are generally mandatory.
ctx.ajax.call is not able to force the call without those certificates.
Moreover, you cannot specify which certificates to use as you are not in a browser environment.
Cookies allow you to authenticate client requests and keep all session information.
If you need to use cookies in your calls, you must set a cookie to keep this information. You will then be able to use it in another call.
ctx.ajax.call({ //First call, get the cookies information
url: "[url]",
method: e.ajax.method.get,
contentType: e.ajax.content.json,
usePassport: true,
ignoreClientCertificate:true,
headers : {
'Authorization': 'Basic [base 64 login:password]',
'X-CSRF-Token': 'Fetch'
},
success: function(res, status, xhr) {
try {
var csrf = xhr.headers['x-csrf-token'];
var cookie = xhr.headers['Set-Cookie'];
var myUrl = "https://domainName/sap/opu/odata/sap/xxx";
ctx.ajax.call({ //Second call, use the cookies in the headers object
url: myUrl,
method: e.ajax.method.post,
ignoreClientCertificate:true,
contentType: 'application/json;charset=utf-8',
headers : {
'Content-type': 'application/json;charset=utf-8',
'X-CSRF-TOKEN': csrf,
'Cookie': cookie
},
success: function(res, status, xhr) {
ctx.log("Good call");
},
error: function(xhr,res,status) {
ctx.log("Something went wrong with the call");
}
});
} catch (ex) {
ctx.log("Something went wrong : " + ex.description);
}
},
});
A token is a piece of data created by a server that contains information to identify a user and the token validity. If you use the token incorrectly in the Ajax call, it may lead to an issue. Like the cookies, the token needs two calls.
For instance, you make an API call in the Factory. You use the first call to get the token. The second call triggers the API call and it creates a job in the job queue. In this case, the calls are linked. The second call happens if the first one is successful.
ctx.ajax.call({ //First call, get the token
url: serviceAccount.urls.accessTokenURL, //Url got from the service key “url parameter + /oauth/token?grant_type=client_credentials”
method: e.ajax.method.get,
contentType: e.ajax.content.json,
header:{
Accept: e.ajax.content.json,
Authorization: 'Basic ' + ctx.base64.encode(serviceAccount.credentials.clientID + ':' + serviceAccount.credentials.clientSecret, false) // clientID and clientSecret got from the service key
},
success: function(res, status, xhr) {
ctx.log('Token generated');
sc.localData.Token = 'Bearer ' + ctx.get(res, 'access_token');
ctx.ajax.call({ // Second call
url: serviceAccount.urls.apiTriggerURL, //Url from the API trigger (end with “/run”)
method: e.ajax.method.post,
contentType: e.ajax.content.json,
header:{
Accept: e.ajax.content.json,
'irpa-api-key': serviceAccount.urls.irpaTriggerToken, //Token get from the API trigger
Authorization: sc.localData.Token
},
data:{
//Payload of your API Trigger
},
success: function(res, status, xhr) {
ctx.log('success');
sc.endStep();
return;
},
error: function(xhr, status, statusText) {
//web service failed
sc.setError(e.error.Fail, 'Web service failed: [' + status + ']' + statusText + ' - ' + xhr.responseText);
sc.endScenario();
return;
}
});
},
error: function(xhr, status, statusText) {
//web service failed
sc.setError(e.error.Fail, 'Web service failed: [' + status + ']' + statusText + ' - ' + xhr.responseText);
sc.endScenario();
return;
}
});
Here are some examples of the best practices for Web Service Calls.
As stated before, due to the complexity of the Ajax call, you may face different issues.
To assess these issues with the Ajax call, you can use multiple tools:
Postman is a tool that allows you to interrogate and test Web services and API calls.
This tool has multiple features and is easy to use.
cURL is a command-line tool that you can use to get or send data including files using URL syntax.
On Windows 10 build 17063, cURL is included by default.
In the Command Prompt, you can test the cURL command during the assessment.
If the test is successful, the agent calls the cURL command through the terminal.
Note : If it does not work even with Postman, it means that you cannot make it work with the Ajax call function.
a. When it works, click the Code button.
b. Choose the programming language you work with (should be "JavaScript - jQuery")
Note: You can see different languages in the red box on the left. Here, we choose the JavaScript-jQuery language because the Ajax call function has been developed with the jQuery.ajax() method. In the green box, you notice the code generated by Postman.
function getCall(success, error, localData){
var result;
var url = localData.fetchAPI = "";
//API call to get csrf token and cookies
ctx.ajax.call({
method: e.ajax.method.get,
url: url,
contentType: e.ajax.content.json,
headers: {
'Authorization': localData.auth,
'x-csrf-token' : 'fetch',
'Cookie' : 'fetch',
'Accept' : 'application/json'
},
ignoreClientCertificate: localData.clientCert,
success: function(res, status, xhr) {
if(res.d){
result = xhr;
success(result);
} else {
result = res;
error(result);
}
},
error: function(xhr, error, statusText) {
result = res;
error(result);
}
});
If you still face issues during the call (issues from the xhr object) after you performed the test in the previous process, try the following tests:
Note : If it still does not work, check the error code, that can lead you to the origin of the error.
By default, the SSL certificate verification is not enabled in Postman. This means that it forces the call even if the certificate is not available.
If the call on Postman does not work after you enabled this option, this means that this certificate is mandatory to make the call. As stated before, the Ajax call does not have this option.
One solution could be to use cURL.
curl --location --request GET "http://XXXX"
var cmdCurl = 'curl -k --location --request GET "http://XXXX"
ctx.exec(cmdCurl, 60, function(reqres) {
var reqdata = ctx.json.parse(reqres.output);
});
Note : The option -k is used to ignore the client certificates.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
19 | |
18 | |
16 | |
10 | |
8 | |
8 | |
7 | |
7 | |
7 | |
6 |