Insert custom http header into request

5747
7
01-14-2016 12:31 PM
NathanielOlson
New Contributor II

I have implemented a proxy in front of my arcgis server. The proxy has its own authentication that requires a custom http header be added to the http request. I have looked at the esriRequest.setRequestPreCallback to try set the headers there, but the ioArgs do not allow me to insert a custom header. Is there a way to insert a custom http header on all esri requests that go through my proxy?

7 Replies
ChrisSmith7
Frequent Contributor

I'm using the ASP.NET proxy - I have custom headers added in the web.config, e.g.

<system.webServer>
       <httpProtocol>
            <customHeaders>
                 <add name="X-Custom-Name" value="MyCustomValue" />
            </customHeaders>
       </httpProtocol>
</system.webServer>

Was this what you're looking to do?

0 Kudos
NathanielOlson
New Contributor II

Thanks Chris. I am not using any of the proxies from Github. We have an ASP.Net web api 2 RESTful endpoint that we built as the proxy. The api is working when I turn off the authentication at our API (proxy).

We need to add an Authorization header with a bearer access token to the http requests made by the ESRI JS api. I've already authenticated the user and have the access token and just need to add a custom http header via javascript before the ESRI request is made. The access token will be different for each user, so can't be set statically in the web.config file.

0 Kudos
ChrisSmith7
Frequent Contributor

Nathaniel,

I did some research - it looks like another developer is doing something similar - would this help?

ArcGIS JavaScript setRequestPreCallback is partially working? - Geographic Information Systems Stack...

Bjorn Svensson​ indicates that you can add your token to the service URL, and the API will send it. Is that an option for you? I'm still yet stubbing my toes in this area...

0 Kudos
NathanielOlson
New Contributor II

I found that post in geonet as well. I've tried appending the token to the url as suggested by Bjorn, but that did not add the Authorization http header that I need. I've also tried using the IdentityManager but couldn't get that to add the token in the way I need it added, either.

In some JS frameworks, you can set up default http headers that are added to every http request made through that framework's http client. I just haven't found a way yet to do that with the ESRI/dojo JS api.

0 Kudos
BenjaminTarica
New Contributor

Any update on your issue ? I have the same problem

0 Kudos
NathanielOlson
New Contributor II

Unfortunately, no update. We ran into enough issues working with the ESRI dojo js that we decided to go a different route with the js map library.

0 Kudos
AliWieckowicz
New Contributor III

I have seen this work :

esriRequest.setRequestPreCallback(function (ioArgs){

                                    if (ioArgs.url.indexOf("api2t.test.org") > -1) {

                ioArgs.headers = ioArgs.headers || {}; // make sure headers are not null

                                                ioArgs.headers["user_key"] = "zcc123thgkjdkfjksdf123jdsj5msd59" // add custom headers

                

                }

               return ioArgs;

0 Kudos