0

There is post request to get token for user . I have used angular $http.post method In my browser network window its not giving me response but when i try this in postman it gives me response here is screen shot attached please help
first image

reponse image

postman image

this.login = function (loginData) {

        var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;

        //if (loginData.rememberMe) {
        data = data + "&client_id=" + constants.AUTHSETTINGS.CLIENT_ID;
        //}
        debugger;
        console.log(data)

        var deferred = $q.defer();
        $http.post(api_url + '/token', data, { 
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
         })
        .then
        (function (response) {
            if (loginData.rememberMe) {
                localStorageService.set('authorizationData', { token: response.data.access_token, userName: loginData.userName, refreshToken: response.data.refresh_token, useRefreshTokens: true });
            }
            else {
                localStorageService.set('authorizationData', { token: response.data.access_token, userName: loginData.userName, refreshToken: "", useRefreshTokens: false });
            }
            console.log('---auth servcie')
            console.log(response)
            authentication.isAuth = true;
            authentication.userName = loginData.userName;
            authentication.useRefreshTokens = loginData.rememberMe;

            deferred.resolve(response);
        }
        ,function (err, status) {
                console.log('auth error -- here ')
                console.log(err)
                console.log(status)
                deferred.reject(err);
                //deferred.resolve(err);
            });


        return deferred.promise;
    };

raw data var data = "grant_type=password&username=asad@gmail.com&password=1234&client_id=angularjs

Cœur
  • 37,241
  • 25
  • 195
  • 267
Asad
  • 3,070
  • 7
  • 23
  • 61
  • why negative vote ? – Asad Sep 29 '17 at 11:35
  • I believe its because you didnt include the js part of the `$http.post` so people cannot really help you – Jaqen H'ghar Sep 29 '17 at 11:37
  • added @JaqenH'ghar – Asad Sep 29 '17 at 11:39
  • can you give answer @JaqenH'ghar – Asad Sep 29 '17 at 11:46
  • Alright its not my downvote but I upvoted you anyway your question is legit. Well not sure about the answer 100% because never tried it before but it looks as you should encode your data.. see this [so link](https://stackoverflow.com/questions/24710503/how-do-i-post-urlencoded-form-data-with-http) good luck – Jaqen H'ghar Sep 29 '17 at 11:47
  • thank you for ur help this looks like my problem https://stackoverflow.com/questions/45431372/api-working-in-postman-but-not-in-angularjs-code – Asad Sep 29 '17 at 11:50
  • I have seen ur link my data is already in that format :( – Asad Sep 29 '17 at 11:52
  • The question is off-topic because there's not enough data. This problem totally depends on the backend and can't be replicated by readers. You're the only person who can debug what's wrong. You've attached request data for $http (without the most important piece, which is `data`) but not for Postman. Compare raw requests line by line for both cases and figure out the difference. – Estus Flask Sep 29 '17 at 12:17
  • hI @estus loginData is only contain user name and password its obvious it is making query string here `var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;` – Asad Sep 29 '17 at 12:27
  • I have posted whole screen for postman you can see the www from encoded data – Asad Sep 29 '17 at 12:29
  • here is duplicate question but not answered https://stackoverflow.com/questions/45431372/api-working-in-postman-but-not-in-angularjs-code – Asad Sep 29 '17 at 12:32
  • Please, show *raw POST request with request payload* for both cases. A screenshot with filled form in Postman doesn't count, it doesn't contain headers, see http://blog.getpostman.com/2015/06/13/debugging-postman-requests/. You'll likely be able to tell what's wrong by yourself after doing that. The said question is not answered because it's also an offtopic that cannot gain a quality answer. – Estus Flask Sep 29 '17 at 12:50
  • raw data is added Header is shown in my js code ` headers: { 'Content-Type': 'application/x-www-form-urlencoded' }` – Asad Sep 29 '17 at 13:05
  • and postman screen shot is shown u can see header there – Asad Sep 29 '17 at 13:06
  • showing on postman inspecting window and on mozila firefix – Asad Sep 29 '17 at 13:22

0 Answers0