Ryan Drake
Ryan Drake

Reputation: 643

Angular: Is this the correct way to structure a $http POST request?

I've got an example curl line here that I've tried to change into a $http POST:

curl -X POST -H "content-type: application/json" -H "AUTH_TOKEN: vLC4grMuKbtbamJL3L6x" localhost:8080/v1/segments?appkey=c2439fb30a1cad03e9e02bd733ef2ad5 -d '{"segment" : {"segment_name" : "ャロットケーキが好き", "segment_type":"static"}}'

Below is the Angular code where you can see that towards the bottom I've got a POST request. I just wanted to clarify if my formatting looked correct or if there's issues with it? I'm particularly interested in knowing if I've passed in the segment_name($scope.geoRegion.name) correctly

   'use strict';
    angular.module('otherLevelsApp').controller('GeoRegionCreateCtrl', [
   '$scope', '$location', 'GeoRegion', 'Hours', '$http', function($scope, $location,      GeoRegion, Hours, $http) {
    console.log('new region controller');
    $scope.app_id = (/apps\/(\d+)/.exec($location.absUrl())[1]);
    $scope.newGeoRegion = true;
    $scope.geoRegionId = '';
    $scope.hours = Hours;
    console.log('$scope.hours', $scope.hours);
    $scope.geoRegion = {
      app_id: $scope.app_id,
      geoRegion_id: '',
      latitude: 37.7879938,
      longitude: -122.40743739,
      name: '',
      address: '',
      radius: 500,
      customer_id: $scope.customer_id
    };
    _.delay((function() {
      return $scope.setupGoogleMap();
    }), 500);
    window.test_scope = $scope;
    return $scope.updateOrAddGeoRegion = function() {
      var region;
      $scope.loading = true;
      console.log('creating new region with ', $scope);
      region = new GeoRegion($scope.geoRegion);
      console.log('region', region);
      return region.$save((function() {
        return $http({
          url: "http://localhost:8080/v1/segments?appkey=" + window.APP_KEY,
          method: "POST",
          data: {
            segment: {
              segment_name: $scope.geoRegion.name,
              segment_type: "static",
              filter: null,
              estimate: null,
              estimated_at: null
            }
          },
          headers: {
            "Content-Type": "application/json",
            Auth_Token: window.AUTH_TOKEN
          }
        }).success(function(data, status, headers, config) {
          return $location.path("/");
        }).error(function(data, status, headers, config) {
          return $scope.errors = {
            "Error": ["There was a problem creating your Geo Segment. Please try again later."]
          };
        });
      }), function(response) {
        if (response.data.errors) {
          $scope.errors = response.data.errors;
        } else {
          $scope.errors = {
            "Error": ["There was a problem connecting with the server. Please try again later."]
          };
        }
        return $scope.loading = false;
      });
    };
  }
]);

Upvotes: 0

Views: 138

Answers (1)

basarat
basarat

Reputation: 275917

I'm particularly interested in knowing if I've passed in the segment_name($scope.geoRegion.name) correctly

Yes

Upvotes: 1

Related Questions