vendredi 14 septembre 2018

How can I send a fetch() request through a proxy?

I have a HTTP proxy that I want to proxy all HTTP requests from my browser client app.

On the command line, I can proxy requests through this:

curl https://code.sgo.to --proxy localhost:5555

Or this:

var http = require("http");

var options = {
  host: "proxy",
  port: 8080,
  path: "http://www.google.com",
  headers: {
    Host: "www.google.com"
  }
};
http.get(options, function(res) {
  console.log(res);
  res.pipe(process.stdout);
});

But, on the client, "path" doesn't seem like an actually valid "path". E.g.

fetch("http://localhost:5555http://code.sgo.to")

Doesn't look right. But

fetch("http://localhost:5555/http://code.sgo.to")

Breaks my standard proxy because there is an extra "/" in the path that it is expecting to be passed an URL.

Would anyone know what's the equivalent of that in the browser (e.g. through fetch() or through XMLHttpRequest)?




Aucun commentaire:

Enregistrer un commentaire