简体   繁体   中英

How do I make a post request with proxy

I am new and I don't know how to make a post request with a proxy in node.js. I tried many methods but nothing works...

I have the proxy host, port, username and password.

How do I do this in node.js?

example: I want to make a post request to http://example.com sending this object {"a": 3} using a proxy.

Thank you a lot for any advice / help.

I found the solution:

const got = require('got');
const tunnel = require('tunnel');

(async () => {
    const { body } = await got.post("example.com", {
        json: {
            "a": 3
        },
        responseType: 'json',
        agent: {
            https: await tunnel.httpsOverHttp({
                proxy: {
                    host: "proxy host",
                    port: 0,
                    proxyAuth: "user:pass"
                },
            }),
        }
    });
    console.log(body);
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM