1

i have a problem on my angular app :

i have a Curl request that work fine in my terminal :

  curl -d "grant_type=password&client_id=IDXXXXXX&client_secret=SECRET&username=USERNAME&password=PASSWORD" https://MYINSTANCE/oauth_token.do

now i want to use it with angular JS.

here's what i tried :

var getDatas = {
  grant_type: "password",
  client_id: "ID",
  client_secret : "PASS",
  username : "USER",
  password : "PASSWORD"
}

$http({
  method: 'GET',
  url: 'https://MYINSTANCE/oauth_token.do',
  data : JSON.stringify(getDatas)
}).then(function successCallback(response) {
  alert("response : " + response);
}, function errorCallback(response) {
  alert("error  : " + response);
});

But the service returns me Error.

I'm a noob on Curl requests with angular, someone can give me some advices?

Thx a lot!

Regards

2
  • In cURL, you are making a POST request with application/x-www-form-urlencoded content-type, not JSON Commented Apr 19, 2016 at 14:43
  • hi sorry but i don't get it... what i'm supposed to do if i just want to send some good parameters to the server? Commented Apr 19, 2016 at 15:00

1 Answer 1

2

Your JS is attempting to send a GET request with data in the body. Your curl request is implicitly using POST when you pass data with -d. Also, by calling stringify, the data you are sending is a JSON string instead of standard POST format. So to get your JS to match your curl request (if you're also using jQuery):

$http({
  method: 'POST',
  url: 'https://MYINSTANCE/oauth_token.do',
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  data : $.param(getDatas)
})...

Without jQuery ($.param) you can write a generic function to turn an object into a POST data string:

var formData = new FormData();
formData.append("grant_type", "password");
formData.append("client_id", "ID");
...
$http({
    ...
    data: formData
})...

Or build the POST string directly:

data: "grant_type=password&client_id=" + client_id + "&secret=" ...
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your answer, but the server still return me error with this configuration.
@HadrienDelphin Forgot we need to convert the data object to POST param format ourselves. Please see my edit.
Ok now i have something strange, in the console (i use chrome inspect), the console return me à 404 error on the url i try to reach. i'm sure it's working, when i copy paste it on chrome, i have the good informations... i feel a bit lost right now...
@HadrienDelphin I made an edit to explain what to do without jQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.