11

I am totally new to AngularJs. I am trying to send a PATCH request using Angularjs to Django Tastypie API's. My code is

var module = angular.module('myApp', []);

module.config(function ($httpProvider) {
});

function MyController($scope,$http)
{
$scope.patchCall=function(){
    $http({
    url: "/patchrequest/",
    data:data,
    method: "PATCH",
})
.success(function(data){
    console.log("SUCCESS");
    $scope.list = data.items;
}).error(function() {
    console.log("FAIL");
});
}
}

But when I am trying to send a request using this code I am Getting an error that http.patch is not a function. Tell me how can i configure ng-app and services to send a PATCH request using AngularJs. I read PATCH request is available in $resource so i tired it with $resource also. But find the same result. Please guide me how can i configure an app from scratch to send CRUD requests, specially PATCH request

3
  • What version of Angular are you using? Commented Nov 30, 2013 at 21:12
  • how are you calling the request? Can't see how you would get error http.patch is not a function Commented Nov 30, 2013 at 21:12
  • I am using AngularJs 1.1.5- min.js. I am callding the request is on button click . here i sent a request using Http. but when now i m reading somewhere there written it will go using ngResource. Commented Nov 30, 2013 at 21:28

1 Answer 1

20

Your error doesn't make sense based on the code you're showing, but a common issue with adding PATCH to AngularJS is that it doesn't have a default Content-Type header for that HTTP method (which is application/json;charset=utf-8 for PUT, POST and DELETE). Here's my configuration of the $httpProvider to add patch support:

module.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.patch = {
        'Content-Type': 'application/json;charset=utf-8'
    }
}])
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @Kevin Stone, Today is my first day with AngularJs. So I am totally new., Perhaps I am making some other mistake also.Let me make changes and then see
@Kevin Stone is usage == $http.patch(path, data); ?
$http({method: 'PATCH', ...})
@KevinStone The relevant RFC defines the Content-Type header for JSON Patch as being application/json-patch+json.
Thanks, that's news to me. You'll have determine what content-type your backend is expecting for a PATCH call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.