0

I'm trying to get the data from my Rest API controller using anuglarjs http GET request. However, the params that I need to send include "." symbol which caused the URL failed. I tried to encode it but it does not work. Here is what I did:

function getApplication($http) {
    this.getData = function(application,name) {
      return $http({
        method : 'GET',
        url : http://localhost:8080/app/rest/+ 'app/' + encodeURIComponent(name)
      }).then(function successCallback(response) {
        return response.data;
      }, function errorCallback(response) {
        console.log(response.statusText);
      });
    }
  }

The name param is app.01.com

and the URL result I got is:

GET http://localhost:8080/app/rest/app/app.01.com 406 (Not Acceptable)

anyone know how to encode the url so I can get the data from Rest Controller? Thanks

2
  • Pass name in as an object on the request body. { method : 'GET', url : localhost:8080/app/rest/+ 'app/', data: name } Commented Jan 20, 2017 at 21:47
  • Sounds like a bad implementation on the service side. You could try manually replacing . with %2E. If that works, the service is being too strict in its parsing rules. Commented Jan 20, 2017 at 22:23

3 Answers 3

2

Use btoa and atob

<!DOCTYPE html>
<html>
<body>

<p>Click the button to decode a base-64 encoded string.</p>

<button onclick="myFunction()">Try it</button>

<p><strong>Note:</strong> The atob() method is not supported in IE9 and earlier.</p>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "Hello World!";
    var enc = window.btoa(str);
    var dec = window.atob(enc);

    var res = "Encoded String: " + enc + "<br>" + "Decoded String: " + dec;
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

function getApplication($http) {
    this.getData = function(application,name) {
      return $http({
        method : 'GET',
        url : http://localhost:8080/app/rest/+ 'app/' +  window.btoa(name)
      }).then(function successCallback(response) {
        return response.data;
      }, function errorCallback(response) {
        console.log(response.statusText);
      });
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Have you try to put the url datas into double quotes ? url : "http://localhost:8080/app/rest/+ 'app/' + encodeURIComponent(name)"

Comments

0

It is not possible that "." is an unreserved character and that "URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent". Therefore, /%2E is the same as /. , and that will get normalized away.

then you can use localhost:8080/app/rest/app/app%2E01.com as your urls

Comments

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.