I'm doing a get REST api request to a server. The server houses automation jobs what I'm looking to kick off with parameters.
var jobURL = "https://hostname/api/v1/tasks";
$.ajax({
url: jobURL,
method: 'GET',
async: false,
contentType: "application/json",
data: {
name: '*JOBNAME*'
},
headers: {
'Authorization': 'Bearer '+ obj.access_token,
'Content-type': 'application/x-www-form-urlencoded',
Accept:'application/json'
},
success: function(data){
taskname_obj = data // result
}
I'm doing this per the documentation of the server product and it is working.
I have a successful request, with items returned in the 'taskname_obj' variable. With that said I'm trying to use the same sort of code for another call.
// THIS IS WORKING TO JOB WITHOUT PARAMS.
// Let's run the job.
$.ajax({
url: jobURL,
method: 'POST',
async: false,
headers: {
'Authorization': 'Bearer '+ obj.access_token
},
success: function(data){
taskname_obj2 = data // result
}
})
SO I'm doing the above code
Again I am success, I can start my job. I have indication of success returned into taskname_obj2.
With all that said, I am trying to start this with parameters. I suppose I have to put that into the "data:" item, and I shout use JSON. So... I do the following...
var payload = {
USER_ID: "",
BEGDATE: "060121",
ENDDATE: "121521"
};
$.ajax({
url: jobURL,
method: 'POST',
async: false,
contentType: 'application/json',
data: payload,
headers: {
'Authorization': 'Bearer '+ obj.access_token,
'Content-type': 'application/x-www-form-urlencoded',
Accept:'application/json'
},
success: function(data){
taskname_obj2 = data // result
}
})
This doesn't work. I am getting a 415 error back from the server for unsupported media type. I also tried not using JSON.stringify for the data item but I also get a 415 with that.
I think I'm not telling it correctly how to read my JSON. Can anybody help me understand what I'm doing wrong?
I also tried adding this contentType: 'application/json', to no avail.
UPDATE: Using BARMARs technique returns me a 422 error.
$.ajax({
url: jobURL,
method: 'POST',
async: false,
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(payload),
headers: {
'Authorization': 'Bearer '+ obj.access_token
},
success: function(data){
taskname_obj2 = data // result
}
})
Get rid of the Content-type and Accept headers. Use the contentType: and dataType: options to specify these.
And since the API requires JSON input, it should be contentType: "application/json"
$.ajax({
url: jobURL,
method: 'POST',
async: false,
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(payload),
headers: {
'Authorization': 'Bearer '+ obj.access_token
},
success: function(data){
taskname_obj2 = data // result
}
})