angularjs - Call to a REST API cause 415 (Unsupported Media Type) -
am trying integrate service web app, did making post call got error:
post https://admin.example.com/api/v1/service 415 (unsupported media type)
am not http codes tried figure out 415 means didn't reall doing simple:
.controller('createbeaconctrl', function($scope, $state, $http) { $scope.item = { value1: $scope.value1, value2: $scope.value2, value3: 1 }; var url = 'https://admin.example.com/api/v1/service'; $scope.submit = function() { $http.post(url, $scope.item). success(function(data, status, headers, config) { // callback called asynchronously // when response available console.log("done"); }). error(function(data, status, headers, config) { // called asynchronously if error occurs // or server returns response error status. console.log("error making call"); }); }; });
am authenticating call sending username , password header:
$httpprovider.defaults.headers.post = { username: 'e9fbcaf4edsfdisfhsdhfds', password: 'fdsfgfgreer4' };
i checked header
username
, password
there , the values of $scope.item
in payload.
i have 2 enquiries first think it's not secure hard code username , password proper way it? had cors problem turned off chrome security rid of because if apis meant called different domains why got cors error. thank in advance
posting using angularjs not automatically infer 'application/json' header tell server @ admin.example.com parse data json. 415 indicates server expecting json data , set unknown. try bit this:
$http({ method: 'post', url: 'https://admin.example.com/api/v1/service', headers: { 'content-type': 'application/json' }, data: { test: 'test' } });