0

I currently have JSON like the following (from the Australia Post developer guide):

"PickupDetails_v1.Header": 
{ "Common_v1.UserName": "testuser", 
"Common_v1.AccessKey": "xxxxxxxxx", 
"Common_v1.CustomerID": 123 }

I'd like to know what the best approach is to being able to have an object in JS without the namespaces (e.g. PickupDetails_v1) when retrieving data from the API and vice versa when sending data. Basically I'd like to be able to know the best way to be able to customize JSON property names in Angular.

For example:

var pickupDetails = { UserName: "testuser"; AccessKey: "xxxxxxx" }; 
$http.post(pickupDetails, "http://www.example.com/postageDetails"); // Should send the pickup details with the namespaces).

var storedPickupDetails = $http.get("http://www.example.com/postageDetails"); // Should have the same properties as pickupDetails.
ACOMIT001
  • 510
  • 1
  • 7
  • 21

1 Answers1

1

I would do this with interceptors.

The below example is taken from the documentation for $http and modified slightly for brevity.

$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      return config;
    },

    // optional method
    'response': function(response) {
      // do something on success
      return response;
    },
  };
});

$httpProvider.interceptors.push('myHttpInterceptor');

The config and response objects will contain all data about requests and responses and you could hook into the data property of those objects and change the keys to add/remove the namespace

ivarni
  • 17,658
  • 17
  • 76
  • 92