Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP(S) Proxy | Agent With OhMyFetch #161

Closed
BlackYuzia opened this issue Nov 12, 2022 · 4 comments · Fixed by #173
Closed

HTTP(S) Proxy | Agent With OhMyFetch #161

BlackYuzia opened this issue Nov 12, 2022 · 4 comments · Fixed by #173
Labels
documentation Improvements or additions to documentation

Comments

@BlackYuzia
Copy link

BlackYuzia commented Nov 12, 2022

Hi there.

Just a short question.
How we could use ohmyfetch with HTTP(S) Proxy | Agent?

For example.
If we use axios, we could use options.httpsAgent with httpsAgent

In ohmyfetchI didn't find any docs about proxy and/or httpsAgent

@yuyinws
Copy link
Contributor

yuyinws commented Nov 20, 2022

It's works for me on node 16.X

import HttpsProxyAgent from 'https-proxy-agent'
import { ofetch } from 'ofetch'

await ofetch('user',{
 agent: new HttpsProxyAgent('http://127.0.0.1:7890')
})

@BlackYuzia
Copy link
Author

BlackYuzia commented Nov 20, 2022

Yes, this worked for me too. Thx.

Suppose, devs should declare agent option in fetch options.

@pi0
Copy link
Member

pi0 commented Nov 21, 2022

PR welcome to the docs.

@NozomuIkuta NozomuIkuta added the documentation Improvements or additions to documentation label Jan 12, 2023
@289997171
Copy link

289997171 commented Dec 17, 2023

server\api\test\google.get.ts

import {HttpsProxyAgent} from "https-proxy-agent";
import {$fetch, ofetch} from "ofetch";
const agent = new HttpsProxyAgent('http://127.0.0.1:7890')

export default defineEventHandler(async (event) => {
    // const res = await ofetch('https://www.google.com', {
    //     agent: agent
    // })
    const res = await $fetch('https://www.google.com', {
        agent: agent
    })
    console.log(res);
    return res
})

curl test request

curl http://localhost:3000/api/test/google

{"url":"/api/test/google","statusCode":500,"statusMessage":"","message":"[GET] \"https://www.google.com\": <no response> fetch failed","stack":"<pre><span class=\"stack internal\">at process.processTicksAndRejections (node:internal/process/task_queues:95:5)</span>\n<span class=\"stack internal\">at async $fetchRaw2 (/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:228:14)</span>\n<span class=\"stack internal\">at async $fetch2 (/node_modules/ofetch/dist/shared/ofetch.00501375.mjs:261:15)</span>\n<span class=\"stack\">at <anonymous> (\\server\\api\\test\\google.get.ts:9:1)</span>\n<span class=\"stack internal\">at async Object.handler (/node_modules/h3/dist/index.mjs:1675:19)</span>\n<span class=\"stack internal\">at async Server.toNodeHandle (/node_modules/h3/dist/index.mjs:1885:7)</span></pre>"}
C:\Users\Administrator>

I'm use 'Clash for windows', I have tested with java http proxy, and it work fine. but i dont know why it not work with ofetch or $fetch.

"nuxt": "^3.8.2",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants