Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Convert curl to Angular $http POST request

I have this curl line:

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"}}'

Now that curl directly hits the API. I'd like to be able to do a $http POST after the save and redirection back to route 'return region.$save((function() {':

'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 $location.path("/");
  }), 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;
  });
};

} ]);

Do you know how I can format it correctly in a $http POST? In particular, do I need to refactor the success and error into the $http code? Ie. Success redirects to return $location.path("/") or...?

I imagine it's something along these lines, but unsure how to finish it off:

$http({
method: 'POST',
url: '',
data: '',
headers: {'Content-Type': 'application/JSON'}

});

Though I am reading Angular doc here: https://docs.angularjs.org/api/ng/service/$http#post I am still a bit confused as to how to directly hit this curl with a $http post?

Appreciate any help!

like image 561
Ryan Drake Avatar asked Feb 25 '26 10:02

Ryan Drake


1 Answers

You're asking for any help, so here's some. Not sure if I understand what you want to do in case of success, but in order to finish off, you can start with

$http({
        url: 'localhost:8080/v1/segments?appkey=c2439fb30a1cad03e9e02bd733ef2ad5',
        method: 'POST',
        data: {'segment' : {'segment_name' : 'ャロットケーキが好き', "segment_type":"static"}},
        headers: {'Content-Type': 'application/json', 'Auth_Token': 'vLC4grMuKbtbamJL3L6x'}
}).success(function (data, status, headers, config) {
            //handle success
            $location.path('/'); //maybe you want to do this  
}).error(function (data, status, headers, config) {
            //handle error
});
like image 122
Kevin Le - Khnle Avatar answered Feb 27 '26 00:02

Kevin Le - Khnle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!