0

While using the following code, I get

400 Bad request rest_missing_callback_params

$scope.signUp = function () {
    var data = {
    	email: $scope.email,
    	password: $scope.password,
    	first_name: $scope.fName,
    	last_name: $scope.lName,
    	username: $scope.uName,
    	billing: {
    		first_name: $scope.fName,
    		last_name: $scope.lName,
    		company: $scope.cName,
    		address_1: $scope.address1,
    		address_2: $scope.address2,
    		city: $scope.city,
    		state: $scope.state,
    		postcode: $scope.pcode,
    		country: $scope.country,
    		email: $scope.email,
    		phone: $scope.mobile,
    	},
    	shipping: {
    		first_name: $scope.fName1,
    		last_name: $scope.lName1,
    		company: $scope.cName1,
    		address_1: $scope.address11,
    		address_2: $scope.address12,
    		city: $scope.city1,
    		state: $scope.state1,
    		postcode: $scope.pcode1,
    		country: $scope.country1,
    	}
    }

    console.log(data)
    $http.post("https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers", {
    		headers: {
    			'Content-Type': 'application/json',
    			'Authorization': 'Basic ' + window.btoa("username:password")
    		},
    		data: data
    	})
    	.then(function (response) {
    		console.log(response)
    	}, function (response) {
    		console.log(response);
    	});
}

But when I use the following code, it posts the data to the server.

var au = window.btoa("username:password"),
    req = {
    	method: 'POST',
    	url: 'https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers',
    	headers: {
    		'Content-Type': 'application/json',
    		'Authorization': 'Basic ' + au
    	},
    	data: data
    }

$http(req).then(function (response) {
	console.log(response)
}, function (response) {
	console.log(response);
});

What is the difference between these two? Why does it happen like so?

3
  • check the developer tools network tab ... see what is sent in each request, if you spot the difference, you'll have your answer Commented Mar 16, 2017 at 5:08
  • Even though iam sending email and password it gives the error missing email and password Commented Mar 16, 2017 at 5:50
  • so, there's absolutely no difference in the request headers or the post parameters? Commented Mar 16, 2017 at 5:51

1 Answer 1

1

For the top example to work you need to change this:

$http.post("https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers", {
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Basic ' + window.btoa("username:password")
  },
  data: data
})

To this:

$http.post("https://www.colourssoftware.com/wordpress/wp-json/wc/v1/customers", data, {
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Basic ' + window.btoa("username:password")
  }
})

According the the Angular $http docs (https://docs.angularjs.org/api/ng/service/$http#post) $http.post() has a different method signature (post(url, data, [config]);) than $http() ($http(config)).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.