I’m having a bit of a problem trying to submit URLs that I’m building dynamically in my AngularJS controller code, and then encoding using encodeURIComponent()
Here’s a URL example built out dynamically (prior to encoding, and will not submit via Fiddler):
JavaScript
x
2
1
http://localhost:49479/api/aggr?sid=f68f52614800393fdbef22cc55a7d3d0fea10e655fff6e7573ca&kri=[CDSStress A]:[USD 10Y X -1.25],[CDSStress A]:[USD 1Y X 1.25]&aggrFunc=SUM([CDSStress A]:[USD 10Y X -1.25]),SUM([CDSStress A]:[USD 1Y X 1.25])&dim=Counterparty
2
Same URL, manually encoded with %20 (which does submit to API layer via Fiddler):
JavaScript
1
2
1
http://localhost:49479/api/aggr?sid=f68f52614800393fdbef22cc55a7d3d0fea10e655fff6e7573ca&kri=[CDSStress%20A]:[USD%201Y%20X%201.25],[CDSStress%20A]:[USD%201Y%20X%20-1.25]&aggrFunc=SUM([CDSStress%20A]:[USD%201Y%20X%201.25]),SUM([CDSStress%20A]:[USD%201Y%20X%20-1.25])&dim=Counterparty
2
However, encoding via encodeURIComponent() does not submit to my API layer via Fiddler. Here’s the coded URL:
JavaScript
1
2
1
http%3A%2F%2Flocalhost%3A49479%2Fapi%2Faggr%3Fsid%3Df68f52614800393fdbef22cc55a7d3d0fea10e655fff6e7573ca%26kri%3D%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%201.25%5D%2C%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%20-1.25%5D%26aggrFunc%3DSUM(%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%201.25%5D)%2CSUM(%5BCDSStress%20A%5D%3A%5BUSD%201Y%20X%20-1.25%5D)%26dim%3DCounterparty
2
and my angular controller code which builds out the URL and submit to datacontext layer :
JavaScript
1
21
21
1
function sendAggrRequest(kriList, aggrFunc, dim) {
2
3
var results = [];
4
var rageVars = $rootScope.rageSessionVars;
5
var url = "http://" + rageVars.domainName + ":" + rageVars.port + "/api/aggr?sid=" + rageVars.sessionID +
6
"&kri=" + kriList + "&aggrFunc=" + aggrFunc + "&dim=" + dim;
7
8
url="http://localhost:49479/api/aggr?sid=a74b9822cf5e0e75b0d8ff0c25981a573606893150348d6cad80&kri=[CDSStress%20A]:[USD%201Y%20X%201.25],[CDSStress%20A]:[USD%201Y%20X%20-1.25]&aggrFunc=SUM([CDSStress%20A]:[USD%201Y%20X%201.25]),SUM([CDSStress%20A]:[USD%201Y%20X%20-1.25])&dim=Counterparty"
9
datacontext.sendAggrRequestToServer(encodeURIComponent(url)).then(function (data) {
10
if (data.status == 'FAIL') {
11
if (data.messages.length > 0) {
12
logErr("Error retrieving KRI list: " + data.messages[0]);
13
return;
14
}
15
}
16
else {
17
results = data.data;
18
}
19
});
20
}
21
If you can provide some advice on the best way to encode URLs, that would be great.
thank you.
Bob
Advertisement
Answer
Try
JavaScript
1
2
1
encodeURI(uri)?
2
or may be you can just regex replace
space
with %20