Chris
Chris

Reputation: 262

AngularJS POST json to SilverStripe API

I wrote a pretty basic API using a SilverStripe module (here) and while building it I was testing using Postman and Advanced REST Client chrome extension so I know the endpoints work.

Now when trying to reach the endpoints (POST json) the API is telling me that required values aren't set. I'm did a little bit of digging and compared the request headers from Postman to the ones from Angular using Firebug. The only noticeable discrepancy is that in Postman the header for Content-Type is:

"application/json"

and for Angular it's:

"application/json; charset=UTF-8"

Here's the code for the Angular POST (pretty basic):

notebookFactory.addNotebook = function(title) {

    var notebook = "Testing Title 23!";
    var message = {
        Title: notebook
    };

    return $http({
        url: 'api/notebook',
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
            data: message
    });     
};

The API error says: "The JSON property Title is required"

Is this something that could make a difference? I've tried adding charset=UTF-8 to the end of the Content-Type in Postman and receive the same error. Is there any way to remove the charset from the Angular POST header?

Let me know if you need any more info and thanks in advance!

Upvotes: 3

Views: 395

Answers (1)

Chris
Chris

Reputation: 262

This was a small issue with the code for handling json in the RESTful API module.

The developer has made the fix already and can be referenced here: https://github.com/pstaender/silverstripe-restful-api/issues/1

Upvotes: 1

Related Questions