0

Here is my API controller method

 [HttpPut]

        public List<Employee> PutEmployee(int EmpID, Employee empl)
        {
            empl.EmpID = EmpID;
            int index = emp.GetEmployees().FindIndex(i => i.EmpID == empl.EmpID);
            emp.GetEmployees().RemoveAt(index);
            emp.GetEmployees().Add(empl);
            return emp.GetEmployees();
        }

Here is angularJS controller

   $scope.UpdateEmp = function () {
        var empl = $scope.Employee;
        empHttpFactory.update(empl.EmpID,empl ).success(function () { // update method in my resource uses $resource
            $location.path('/EmpList'); //redirect to list
        });

    };

Here is my service code

 var resource={
        update: function (ID, empl) {

           // var empl = {'EmpID':1,'FirstName':'Kailash', 'Salary':62000};
            var params = { EmpID: ID, empval: empl };

                         // return $http.put('../api/Emp/Put', { EmpID: ID , empval:   return $http({
                method: 'PUT',
                url: '../api/Emp/Put',
                data: params

            })

empl });

        }
    }

    return resource;

When i do the debug, i could see the value for EMPID and empval in the angularjs controller and service but when it comes to API controller part, i m getting the value for EMPID alone but not for empval, its coming as null. can someone provide me working sample of it. i m new to AngularJS

Update 1 :

I have replaced the Params to data in my http call. My API method expecting the parameter in the body as per my controller method. I changed the http call accordingly and it started working fine. Also in my first argument to the controller method, I m using the variable name as EmpID, if I have used as "id", then probably, id would have been passed as the parameter in the URL itself

 return $http({
                    method: 'PUT',
                    url: '../api/Emp/Put',
                    data: params

                })

Updated Question: This is regarding the $resource

In $resource documentation, 2nd argument is parameter and the 3rd has list of actions where I can mention the parameters. But if you see from my example, I haven't explicitly used params option but passing data to my controller method. Its working as expected because its taking it as data. Why it so???

$resource('../../Employee/PutEmployee/', { }, { update: { method: 'PUT', empval: { "EmpID": 1, "FirstName": "John", "LastName": "Peter", "Salary": 60000 } ,isArray: true } })

3
  • 2
    What is the REST api supposed to receive. If you had to document this REST service, what would you say about its URL, the body of the request, etc.? Commented May 20, 2015 at 17:13
  • @JB Nizet, I updated my question with the solution I used and also I have another question which I posted Commented May 21, 2015 at 14:08
  • @JBNizet, I updated my question with the solution I used and also I have another question which I posted Commented May 21, 2015 at 14:08

2 Answers 2

2

Here is an example of my toying web api project.

public class MoviesApiController: ApiController {
[HttpGet]
public IEnumerable<Movie> Get() { /* your implementation here */ }
[HttpGet]
public Movie Get(int Id) { /* your implementation here */ }
[HttpPut]
public HttpResponseMEssage Put(int Id) {/* your impl */ }
[HttpPost]
public HttpResponseMessage Post(Movie movie) {/* impl */}
[HttpDelete]
public HttpResponseMessage Delete(int id) { /*impl */ }

In fiddler, calll get with endpoint like: api/MoviesApi/

Call with specific movie id with endpoint like: api/MoviesApi/1

To issue a put, in fiddler change action to put, and rest endpoint like: api/MoviesApi/1

The same url goes for post and delete. Make sure you change it appropriately in fiddler

As for angularjs, I use $resource instead of $http, but your url should be the same. An example of my angularjs service:

angular.module("your_name").factory("your_name", ['$resource', function($resource) {
return $resource('/api/MoviesApi/:Id', null, {
'update' : {method: 'PUT'}});
Sign up to request clarification or add additional context in comments.

7 Comments

But for update(Put), you need to pass the model as well rit? Atleast in my case, i m passing the model as well for update. So in this case, how i can decorate my API and how i call it in angular.
You can do like @JB Nizet's answer for using $http, I am just showing how you use the url because in your question, your backend put method's name is PutEmployee but in your angularjs, the url is api/Employee/Put and I think that's why you get the error "the request resource does not support http method put"
But it says the message as but it says the message as “{"Message":"The requested resource does not support http method 'PUT'."}
In fiddler, did you change your verb to put?
In don't have fiddler with me now, but if I remeber correctly, in fiddler in composer tab, on the left of the url input, there is a drop down list that let you change from get, post, delete,..bla bla. Also, as JB Nizet's comment, looks like you have problem with interpret webapi
|
0

If I understand correctly, the REST service is bound to the URL api/Emp/:EmpID, with method PUT, and expects to receive the new employee data as JSON, in the request body.

But that's not what you're doing in your angular code. You're sending a PUT request on the URL ../api/Emp/Put?EmpID=<theID>&empval=<the employee>, without any request body.

What you need to do is

$http({
    method: 'PUT',
    url: '../api/Emp/' + ID,
    data: empl
});

or, simpler:

$http.put('../api/Emp/' + ID, empl);

5 Comments

I decorated my Put method like this [HttpPut] [Route("~/api/Emp/ID")] and i changed my service code as return $http({ method: 'PUT', url: '../api/Emp/' + ID, data: empVal }) but in fiddler it shows the message as {"Message":"The requested resource does not support http method 'PUT'."}
OK. It seems that your problem is more about how to implement your backend and interpret the annotations (which I assume are C# annotations), rather than an AngularJS problem. Please tag your question with the appropriate tags (.Net, C#, I guess).
Are you doing RESTful, if so, I think you may need to change your api action from PutEmployee to Put then you use url like : '..api/Emp/:Id'. Or you can keep it the way it is which in your case use custom action name, and in your javascript, change url to something like ../api/PutEmployee/:ID
@JackyNguyen can you please share me simple working sample with API controller method and how it needs to be called in $http as well
@JBNizet, hi, i have posted another query related to angularjs, could you help me with it stackoverflow.com/questions/33654437/angularjs-resource-cache

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.