Using Credential.token in REST API

1095
5
02-02-2021 10:39 PM
MarkHolm
New Contributor II

I want to run multiple concurrent count queries on the same layer but the sdk does not provide that, so I turned to the REST API which does. I would like to use the existing Credential.token, but it doesn't work because it requires referer: runtimeqtsdk as a header which I cannot set using XMLHttpRequest.

Any suggestions on using this token so I don't have to manage tokens myself? Seems like there should be a reason the sdk allows access to the token and referer of the Credential.

Thanks

0 Kudos
5 Replies
JamesBallard1
Esri Regular Contributor

Hi @MarkHolm .  How many concurrent count operations are you looking to run? Rather than trying to access the rest API directly, you can spin up multiple API objects and run one async task per object. Each object will persist the result once completed. I realize that's a workaround, but it might be easier to trying to reuse API tokens with the REST Api.

Can you please share which specific API call you're wanting to call concurrently?

0 Kudos
MarkHolm
New Contributor II

I am running a count query on a whereClause with many non unique id's in an in statement on layer to determine if the feature query will exceed the maxRecordCount. The count is more useful than the transferLimitExceeded bool returned on the FeatureQueryResult, so I can estimate how many smaller queries to break it down into. Feature querying can run concurrent operations, but sadly count queries cannot.

 

Specifically I am using queryFeatureCount on the FeatureTable

0 Kudos
GuillaumeBelz
Esri Contributor

Hi @MarkHolm 

Did you try to use setRequestHeaderhttps://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader

Something likes (not tested):

var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE)
console.log(request.getAllResponseHeaders());
}
request.open("GET", url);
request.setRequestHeader("referer", credential.referer);
request.send();

If this doesn't work, that means the support of XMLHttpRequest in Qt is not correct. In this case, you can use QNetworkAccessManager in C++ for theses requests.

0 Kudos
MarkHolm
New Contributor II

I have and it does not work because referer is one of the forbidden header names in the XMLHttpRequest specification.

https://xhr.spec.whatwg.org/#the-setrequestheader()-method

I thought about using QNetworkAccessManager, but the multiple API objects should be an easier approach.

I also tried looking at the AuthenticationManager or credential to see if I could generate a token not using referer: runtimeqtsdk, but the only method I found was removing the referer from the credential after the token is generated which doesn't work

GuillaumeBelz
Esri Contributor

Good point for the forbidden header names.

If the solution proposed by @JamesBallard1  works fine, this is the easiest way to do. And you can use the C++ solution only if you have a performance issue.

0 Kudos