Comments (7)
Same
nuxt #24799
from ofetch.
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.
In addition to this, there is no types for agent
in oftech
Edit this has been reported here
from ofetch.
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.
Could it be because
ofetch
takes preference on nodes native fetch, which in newer versions of node usesundici
which does not useagent
instead it usesdispatcher
.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.
Could it be because
ofetch
takes preference on nodes native fetch, which in newer versions of node usesundici
which does not useagent
instead it usesdispatcher
.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.
Using proxyagent from undici works fine for HTTP proxy. I didn't find a way for HTTPS proxies.
from ofetch.
Related Issues (20)
- Pick function HOT 1
- Typed API definition HOT 12
- Response does not contain headers HOT 2
- Receiving server-sent events (SSE) HOT 2
- Failed to upload Mega-sized base64 image HOT 1
- Go-like syntax to handle errors HOT 6
- error.statusText & error.statusMessage are undefined after Nuxt build but are set in dev
- deprecate `params` alias for `query` HOT 1
- Can access to typed data in interceptors HOT 2
- Set cookies HOT 11
- error on Response.blob()
- Multipart image upload not working with fetch but with RapidAPI HOT 4
- Missing types in node export HOT 1
- `body` vs `params` option HOT 1
- Inconsistent headers object returned after a retried request
- TS2305: Module 'ofetch' has no exported member FetchOptions HOT 1
- Errors in Interceptors are silently handled
- How to handle errors body HOT 1
- GraphQL Support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ofetch.