Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change Content-type to "application/json" POST method, RESTful API

Tags:

angularjs

I am new at AngularJS and I needed your help.

All I need just need is to POST my json to the API and recieve the proper response.

Here's my JSON where i don't know where to code this.

JSON

{      "userId"      :"testAgent2",     "token"       :"testAgent2",     "terminalInfo":"test2",     "forceLogin"  :"false" } 

NOT SURE IF I'm doing this right.

CONTROLLER.JS

function UserLoginCtrl($scope, UserLoginResource) {     //Save a new userLogin     $scope.loginUser = function() {         var loggedin = false;         var uUsername = $scope.userUsername;         var uPassword = $scope.userPassword;         var uforcelogin = 'true';          UserLoginResource.save();     } } 

SERVICES.JS

angular.module('UserLoginModule', ['ngResource'])     .factory('UserLoginResource', function($resource, $http) {         $http.defaults.useXDomain = true;         delete $http.defaults.headers.common['X-Requested-With'];         $http.defaults.headers.post["Content-Type"] = "application/json"; //NOT WORKING          return $resource('http://123.123.123.123\\:1234/SOME/LOCATION/THERE', {}, {             save: {                  method:'POST',                  headers: [{'Content-Type': 'application/json'}]              } //NOT WORKING EITHER         });     }); 

INDEX.HTML

<html ng-app>  <head> <script src="js/lib/angular/angular.js"></script> <script src="js/lib/angular/angular-resource.js"></script> </head>   <body ng-controller="UserLoginCtrl">    <form class="form-horizontal" name="form-horizontal" ng-submit="loginUser();">  <div class="button-login">  <!-- start: button-login --> <button class="btn btn-primary" type="submit">Login</button>  </div>  </form>   </body>      </html> 

I kept on getting a response like Unsupported Media Type. I don't know, what else to do.

like image 935
user2073913 Avatar asked Jun 20 '13 10:06

user2073913


People also ask

How do I change the Content-Type to application JSON in Postman?

You can set a content type header manually if you need to override the one Postman sends automatically. You can use variables in your body data and Postman will populate their current values when sending your request. To beautify your XML or JSON, select the text in the editor and then select ⌘+Option+B or Ctrl+Alt+B.

How do you set the Content-Type of the response to JSON?

The correct MIME media type for JSON is application/json . JSP will use it for sending a response to the client. Show activity on this post. “ application/json ” is the correct JSON content type.

How do I POST JSON to a REST API endpoint?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.


1 Answers

Assuming you are able to use one of the more recent "unstable" releases, the correct syntax to change the header is.

app.factory('BarService', function ($resource) { var BarService = $resource('/foo/api/bars/:id', {}, {         'delete': {         method: 'DELETE',         headers: {             'Content-Type': 'application/json'         }     } });  return BarService; }); 

I find the $resource service is a tremendously powerful tool for building applications and has matured to a point that you do not need to fall back to $http as much. Plus its active record like patterns are damn convenient.

like image 107
acg Avatar answered Oct 05 '22 20:10

acg