Giter VIP home page Giter VIP logo

Comments (7)

kevindaizj avatar kevindaizj commented on August 10, 2024

Same
nuxt #24799

from ofetch.

blowsie avatar blowsie commented on August 10, 2024

Same for me, reproduction here:
Im not sure how we can make a reproduction on Codesandbox for proxy issues? Maybe there is a public proxy we can use?

a node project

import fetch from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';

(async () => {
    const targetUrl = 'https://ident.me/ip';
    {
        const response = await fetch(targetUrl);
        const text = await response.text();
        console.log(text);
    }
    {
        const response = await fetch(targetUrl, {
            agent: new HttpsProxyAgent('http://127.0.0.1:7890')
        });
        const text = await response.text();
        console.log(text);
    }
})();

result: 113.248.xxx.xxx 5.252.198.49 two ips are not the same

but if use nuxt server\api\test\google.get.ts

import {HttpsProxyAgent} from "https-proxy-agent";
import {$fetch, ofetch} from "ofetch";
import fetch from 'node-fetch'

export default defineEventHandler(async (event) => {
    const ip1 = await $fetch('https://ident.me/ip');
    console.log(ip1);

    const ip2 = await $fetch('https://ident.me/ip', {
        agent: new HttpsProxyAgent('http://127.0.0.1:7890')
    })
    console.log(ip2);

    const ip3 = await ofetch('https://ident.me/ip', {
        agent: new HttpsProxyAgent('http://127.0.0.1:7890')
    })
    console.log(ip3);

    const response = await fetch('https://ident.me/ip', {
        agent: new HttpsProxyAgent('http://127.0.0.1:7890')
    })
    const ip4 = await response.text();
    console.log(ip4);

    return {
        ip1,
        ip2,
        ip3,
        ip4
    }
})

{ "ip1": "113.248.xxx.xxx", "ip2": "113.248.xxx.xxx", "ip3": "113.248.xxx.xxx", "ip4": "113.248.xxx.xxx" } the ips are the same, http proxy it's not work

Originally posted by @289997171 in nuxt/nuxt#24799 (comment)

from ofetch.

blowsie avatar blowsie commented on August 10, 2024

In addition to this, there is no types for agent in oftech


Edit this has been reported here

from ofetch.

blowsie avatar blowsie commented on August 10, 2024

Could it be because ofetch takes preference on nodes native fetch, which in newer versions of node uses undici which does not use agent instead it uses dispatcher.

So you can try this.

import { ofetch } from 'ofetch'
import { ProxyAgent } from 'undici'

const res1 = await ofetch('http://ip-api.com/json')

const res2 = await ofetch('http://ip-api.com/json', {
  dispatcher: new ProxyAgent('http://127.0.0.1:7890'),
})

console.log('No Proxy IP: ' + res1.query)
console.log('Proxy IP: ' + res2.query)

from ofetch.

qisuwan avatar qisuwan commented on August 10, 2024

Could it be because ofetch takes preference on nodes native fetch, which in newer versions of node uses undici which does not use agent instead it uses dispatcher.

So you can try this.

import { ofetch } from 'ofetch'
import { ProxyAgent } from 'undici'

const res1 = await ofetch('http://ip-api.com/json')

const res2 = await ofetch('http://ip-api.com/json', {
  dispatcher: new ProxyAgent('http://127.0.0.1:7890'),
})

console.log('No Proxy IP: ' + res1.query)
console.log('Proxy IP: ' + res2.query)

This is also a solution. Currently, I'm using node-fetch instead of fetch in Nuxt, it work well. However, there's a usage example in the README involving an agent. If it's not supported, then shouldn't mention this feature.

from ofetch.

blowsie avatar blowsie commented on August 10, 2024

Could it be because ofetch takes preference on nodes native fetch, which in newer versions of node uses undici which does not use agent instead it uses dispatcher.

So you can try this.

import { ofetch } from 'ofetch'
import { ProxyAgent } from 'undici'

const res1 = await ofetch('http://ip-api.com/json')

const res2 = await ofetch('http://ip-api.com/json', {
  dispatcher: new ProxyAgent('http://127.0.0.1:7890'),
})

console.log('No Proxy IP: ' + res1.query)
console.log('Proxy IP: ' + res2.query)

I can confirm this is working perfectly fine.

from ofetch.

niicojs avatar niicojs commented on August 10, 2024

Using proxyagent from undici works fine for HTTP proxy. I didn't find a way for HTTPS proxies.

from ofetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.