Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS $resource save() causes HTTP Error 415 (Unsupported Media Type)
    primarykey
    data
    text
    <p>I am using Angular 1.2-RC2 (also tried 1.0.8 and 1.1.x) and the ngResource module. The backend is a Spring WebMVC application.</p> <pre><code>angular.module("dox", ['ngResource']) .controller('SettingsController', function ($scope, Settings) { $scope.settings = Settings.query(); $scope.save = function () { Settings.save(); }; }) .factory('Settings', function ($resource) { return $resource('/api/v1/settings/:settingId', {settingId: '@id'}, { 'save': { method: 'POST', headers: { 'Content-Type': 'application/json' } } }); }); </code></pre> <p>Whenever the save() method on the Settings class gets called the frontend receives a HTTP 415 (Unsupported media type). The reason is that AngularJS send the POST request using the following content type:</p> <pre><code>Content type: text/plain;charset=UTF-8 </code></pre> <p>but the backend expects </p> <pre><code>Content type: application/json;charset=UTF-8 </code></pre> <p>According the <a href="http://docs.angularjs.org/api/ngResource.$resource" rel="nofollow noreferrer">API docs</a> it should be possible to override the header but my settings are somehow ignored. I seems that this is a common problem and as a <a href="https://stackoverflow.com/questions/17211443/angularjs-change-content-type-to-application-json-post-method-restful-api">workaround</a> there are many recommendation to use $http.post instead of $resource.</p> <p>Can you give me any hint how to solve this content type problem using the $resource service?</p> <p>Please find the backend controller code <a href="https://github.com/saw303/dox/blob/angularjs/web/src/main/java/ch/silviowangler/dox/web/rest/RestSettingsController.java#L71" rel="nofollow noreferrer">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload