1

I want to deconstruct this url

this works

$http.post('http://mysrc:8080/calc?country=Usa&city=NewYork', {
          {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==' }
});   

to use params insted

this doesn't work

  $http.post('http://mysrv:8080/calc?', {
                    params: { country: "Usa", city: "NewYork" },
                    headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', }
                });  
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • 1
    Why would you use POST with no data? `params` is for GET – charlietfl Jun 09 '16 at 22:48
  • `$http.get('http://mysrv:8080/calc', {params: {country: 'Usa', city:'NewYork'},headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}});` Does this work? – shamsup Jun 09 '16 at 22:51
  • @ShamSUP I don' t think the problem is the "?" since any extra question mark will be traded as a literal...http://stackoverflow.com/questions/2924160/is-it-valid-to-have-more-than-one-question-mark-in-a-url – watashiSHUN Jun 09 '16 at 23:01
  • Actually I swapped the post method for the get method – shamsup Jun 09 '16 at 23:05

4 Answers4

0

It's janky to use url params on a post. Traditionally data like you have goes in the body, commonly url encoded but in any case should match the header indicating the encoding.

See: HTTP POST with URL query parameters -- good idea or not?

Community
  • 1
  • 1
Mike
  • 2,429
  • 1
  • 27
  • 30
0

I think you've misunderstood how $http.post works. Try this:

 $http({params: { country: "Usa", city: "NewYork" },
        headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==' }})
 $http.post('http://mysrv:8080/calc?');  
Alastair Brown
  • 1,598
  • 8
  • 12
0

This is the correct syntax from the docs.

$http.post('/someUrl', data, config).then(successCallback, errorCallback);

So for your example:

$http.post('/someUrl', null, {
                params: { country: "Usa", city: "NewYork" },
                headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==' }
            });

But as others mentioned - since you don't post any data to the server, why use .post?

Muli Yulzary
  • 2,559
  • 3
  • 21
  • 39
0

Use something like this.

$http({
    method: 'post', 
    url : "https://myserver.com/api/v1/name", 
    data : {
        fname : "Senthil",
        lname : "Kumar"
    }
    headers: {
        'Authorization': 'Basic token_hash'
    }
});
SenthilKumarM
  • 119
  • 1
  • 8