- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2016 08:49 PM
Hi All,
I have tried with GET method in $http service(angularjs). Please some one help me to understand how to work with other methods like PUT,POST,JSONP,etc
Thanks
Pradeep
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2016 01:19 AM
yes you need headers. in headers I have given access token because you can not make REST calls to servicenow without authenticating first. you can also pass username and password but access token makes more sense when are making REST calls from within servicenow.
and to create incident you need to fill the fields right? I am passing short description.
in ui page I have
<input type="text" ng-model="sd"></input>
so it value in the input field will be available in your UI script(controller) as $scope.sd. which I am passing to POST rest call like this
data:{
'short_description':$scope.sd
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2016 09:42 PM
Hi Pradeep,
if I remember I explained you how to use $http in other thread. just change the method name from GET to whatever you want. for example see this snippet from my app.
return $http({
url: 'https://a.wunderlist.com/api/v1/tasks',
method: "POST",
headers: {'Content-Type': 'application/json,charset=utf-8'},
data: {
list_id :12345,
title:"my awusme task"
}
}).then(function(result){
//do your logic with result
},function(error){
//log out error
});
so for POST request you need to include one more property called data as shown above. PATCH similar like POST. just change method from POST to PATCH.
return $http({
url:https://a.wunderlist.com/api/v1/tasks/taskid?revision_id=1 ,
method: "DELETE",
headers: {'Content-Type': 'application/json,charset=utf-8'}
});
for delete you don't need to include data. in my example I have specified which task I want to delete in URL as 1234 and there is one parameter called revision id.
it will be different based on different REST APIs.. to delete something in service now you give url something like this
https://dev12985.service-now.com/api/now/table/{tableName}/{sys_id}
let me know if you have any doubts.
(please mark correct/helpful/like based on impact).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2016 10:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2016 10:44 PM
Hi Pradeep,
you have given wrong url. and you havnt given any authorization.
see below code. it will create an incident .
$scope.createIncident = function(){
alert('hello create');
$http({
method: 'POST',
url: "https://yourinsta.service-now.com/api/now/table/incident",
headers: {
'X-UserToken' : window.g_ck
},
data:{
'short_description':$scope.sd
}
}).
success( function(data, status) {
$scope.resu = data.result;
}).
error ( function(data, status) {
$scope.error= data.error;
});
};
I hope this helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2016 11:42 PM
Is that needed to add headers?
And also please explain
data:{
'short_description':$scope.sd
}
Can you share your UI Page code also