Problem with API Geocoding - Hana XSJS

2181
10
Jump to solution
05-13-2022 03:22 AM
Labels (1)
LeonardoRomanato
New Contributor II

Hi all,

I'm trying to geocode some addresses with API Geocoding, in Hana XSJS. 

I made a GET request to geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?<parameters>, where in the paremeters I inserted

  • Token: the access token
  • address: an example address ('1600 Pennsylvania Ave NW, DC')
  • pjson as output format
    but it seems that the request doesn't return anything. Can someone help me?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LeonardoRomanato
New Contributor II

I solved by myself because the problem was in the Hana Trust Score, I had to create a new trust score and link the configuration file .xshttpdest to make the request.

Thanks to everyone!

View solution in original post

10 Replies
Raul_Jimenez
Esri Contributor

That's strange,

I just did it as you said (same as the Postman example) and it worked:

 

curl --location --request GET 'https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?token=MY_API_KEY&f=pjson&singleLine=1600 Pennsylvania Ave NW, DC'

 

 
but it seems that the request doesn't return anything. Can someone help me?
What do you mean by this? can you share the curl and screenshot? (blurring the API key)
0 Kudos
LeonardoRomanato
New Contributor II

Hi thanks for the answer.

 

req = new $.web.WebRequest($.net.http.GET, "f=pjson&singleLine=" + address +"&token="+access_token);
response = client.getResponse()');
response = client.request(req, dest).getResponse();
jresponse = JSON.parse(response.body.asString());

 

in the 4th row of code I parse the the string into json, but while I access to the response object the code crashes.

It seems like the response is an empty object, maybe because of the wrong request
 

0 Kudos
Raul_Jimenez
Esri Contributor

Have you tried:

try{
   // ...
}catch(e){
   console.log("Error: ", e);
}

 It might be an HTML been responded by the API?

0 Kudos
LeonardoRomanato
New Contributor II

sadly the language is not javascript but XSJS (SAP Hana Javscript Language) so I can't print with console.log().

LeonardoRomanato_0-1652454756437.png

 

Anyway I have a function to write into a SQL table to log in wich point my code stop, that's why I know that's somethig wrong when I access to response object. 

For example the code crashes even in this snippet:

req = new $.web.WebRequest($.net.http.GET, "f=pjson&singleLine=" + address +"&token="+access_token);
response = client.getResponse()');
response = client.request(req, dest).getResponse();
printInLogDB(idStep,'X',response.body.asString());

 

0 Kudos
MathiasKemeter
New Contributor

Since XSJS is a service itself, you can forward the error message as part of the service response and then analyze it in the debug console of your browser. Just checked one of my xsjs project and found this sample code.

function doGet() {  
    try{  
              $.response.contentType = "application/json";  
              $.response.setBody(JSON.stringify(getProperties()));  
    }  
    catch(err){  
              $.response.contentType = "text/plain";  
              $.response.setBody("Error while executing query: [" + err.message + "]");  
              $.response.returnCode = 200;  
    }  
} 

 

0 Kudos
John-Foster
Esri Contributor

The basic operation appears to be correct. Using your exact parameters in a web browser you can see it works (you need to replace your token)

https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=pjso...

 

Response:

{
 "spatialReference": {
  "wkid": 4326,
  "latestWkid": 4326
 },
 "candidates": [
  {
   "address": "1600 Pennsylvania Ave NW, Washington, District of Columbia, 20500",
   "location": {
    "x": -77.036546998208649,
    "y": 38.897675107651253
   },
   "score": 100,
   "attributes": {
    
   },
   "extent": {
    "xmin": -77.037546998208654,
    "ymin": 38.896675107651255,
    "xmax": -77.035546998208645,
    "ymax": 38.89867510765125
   }
  }
 ]
}  

 

1. verify your URL is correct?

2. verify you are URL encoding the address string?

3. verify your token is correct?

--jf
0 Kudos
John-Foster
Esri Contributor

@LeonardoRomanato are you certain line 2 is correct?

 

response = client.getResponse()');
response = client.request(req, dest).getResponse();

 

--jf
0 Kudos
Raul_Jimenez
Esri Contributor

As John highlighted, my two cents would go for the encoding of the address. You can avoid encoding it if you uses a POST request instead.

0 Kudos
Raul_Jimenez
Esri Contributor

@LeonardoRomanato were you able to fix this?

0 Kudos