1

Having one of those dumb moments, and I can't figure out what's wrong.

I am trying to call a PUT (Web API) command.

Code Snippets:

var person = {
  "id": 1
  "name": "John",
  "age": 25,
  "country": Australia
};


var serviceUrl = 'http://myapi/person';

console.log(person);

function updatePerson(id, personObject){
  return $http.put(serviceUrl + '/' + id, personObject).then(
    function(results){
     return results;
  });
};



    updatePerson(1, person).then(
     function(results){
     },
     function(error){
     }
   );

Essentially, when I call the above function, I am getting a 400 (Bad Request) error.

Using fiddler, I am noticing that the data being passed (in the TextView tab) is of the form:

id=1&name=John&age=25&country=Australia

Using Postman, where I am defining the raw JSON content as follows:

{
  "id": 1
  "name": "John",
  "age": 25,
  "country": Australia
}

The PUT command executes successfully.

And in fiddler, the Textview represented the one which I put as above.

Any advice or ideas on where to debug/look at?

P.S. I just noticed that all of my PUT commands are returning a 400 error (which I've tested before and was working all fine). I am not completely sure what changed in my solution, for all the PUT functions to stop working properly.

Update: in Chrome's console log, I can see that the object is defined as follows:

{id: 1, name: John, age: 25, country: Australia}

which when I paste directly to Postman works fine as well.

Batuta
  • 1,684
  • 17
  • 48
  • 62
  • Can you post the headers when you make the call and it works, and when you use angular and it fails? Specifically the "Accept" and "Content-Type" headers, but all would be nice. You don't do any configuration on your $http do you? – Jason Goemaat Nov 18 '14 at 21:20
  • I think I may have resolved the issue through the tip you provided @JasonGoemaat - check the configuration on the $http. – Batuta Nov 18 '14 at 21:34
  • possible duplicate of [Parse JSON in JavaScript?](http://stackoverflow.com/questions/4935632/parse-json-in-javascript) – Batuta Jan 27 '15 at 21:47

0 Answers0