Last active
November 7, 2022 07:54
-
-
Save dunnza/67c7f1705daba5a1bb5f750d5f5766b1 to your computer and use it in GitHub Desktop.
How to proxy api requests with NTLM authentication in a create-react-app project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require("http"); | |
const proxy = require("http-proxy-middleware"); | |
module.exports = function(app) { | |
app.use( | |
"/api", | |
proxy({ | |
agent: new http.Agent({ keepAlive: true }), | |
target: "http://example.com/", | |
onProxyRes(proxyRes) { | |
const header = "www-authenticate"; | |
proxyRes.headers[header] = | |
proxyRes.headers[header] && proxyRes.headers[header].split(","); | |
} | |
}) | |
); | |
}; |
Still fails most of the time. I usually have to make an API call in a new tab (ie http://testEnv/api/bootstrap) to the real site to get any connections to work via the local proxy on any consistent basis. otherwise it just get hung on sending basic auth info over and over again.
@nullberri what is your setup like? I'm running an ASP.NET Core WebAPI application through Visual Studio (IIS Express). I haven't encountered any problems yet ...
@dunnza - thanks very much for sharing this. Worked perfectly for me and got me out of a bind.
@TeonWasTaken I'm glad it helped you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I still do not 100% understand how this works, but the primary issues seem to be
We use an http agent to keep the connection alive, and we use the
onProxyRes
hook to massage the "www-authenticate" header into the correct format.I first found this issue which described the problem, which was then referenced by this pull request. The solution was laid out in the comments. The solution eventually got added to the examples in the repo, but it's not obvious that it's out there.