V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
monster1priest
V2EX  ›  问与答

axios 配置 proxy 请求失败

  •  
  •   monster1priest · 2023-07-19 23:09:07 +08:00 · 844 次点击
    这是一个创建于 520 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想用 axios 通过远程代理去做一个请求,但是一直返回错误。
    我换用 HttpsAgent 就可以,请问这是为啥?

    import axios from "axios";
    
    axios.get('https://www.google.com', {
        proxy: {
            protocol: 'https',
            host: 'xxx.yyy.com',
            port: '443',
            auth: {
                username: '123456',
                password: '123456'
            }
        },
    }).then(r => {
        console.log(r.data)
    }).catch(e => {
        console.error(JSON.stringify(e))
    })
    
    
    {
        "message":"read ECONNRESET",
        "name":"Error",
        "stack":"Error: read ECONNRESET\n    at Function.AxiosError.from (file:///*/axios-test/node_modules/axios/lib/core/AxiosError.js:89:14)\n    at RedirectableRequest.handleRequestError (file:///*/axios-test/node_modules/axios/lib/adapters/http.js:591:25)\n    at RedirectableRequest.emit (node:events:527:28)\n    at ClientRequest.eventHandlers.<computed> (*/axios-test/node_modules/follow-redirects/index.js:14:24)\n    at ClientRequest.emit (node:events:527:28)\n    at TLSSocket.socketErrorListener (node:_http_client:454:9)\n    at TLSSocket.emit (node:events:527:28)\n    at emitErrorNT (node:internal/streams/destroy:157:8)\n    at emitErrorCloseNT (node:internal/streams/destroy:122:3)\n    at processTicksAndRejections (node:internal/process/task_queues:83:21)",
        "config":{
            "transitional":{
                "silentJSONParsing":true,
                "forcedJSONParsing":true,
                "clarifyTimeoutError":false
            },
            "adapter":[
                "xhr",
                "http"
            ],
            "transformRequest":[
                null
            ],
            "transformResponse":[
                null
            ],
            "timeout":0,
            "xsrfCookieName":"XSRF-TOKEN",
            "xsrfHeaderName":"X-XSRF-TOKEN",
            "maxContentLength":-1,
            "maxBodyLength":-1,
            "env":{
                "Blob":null
            },
            "headers":{
                "Accept":"application/json, text/plain, */*",
                "User-Agent":"axios/1.4.0",
                "Accept-Encoding":"gzip, compress, deflate, br"
            },
            "proxy":{
                "protocol":"https",
                "host":"xxx.yyy.com",
                "port":"443",
                "auth":"123456:123456"
            },
            "method":"get",
            "url":"https://www.google.com"
        },
        "code":"ECONNRESET",
        "status":null
    }
    
    3 条回复    2023-07-20 14:18:37 +08:00
    naminokoe
        1
    naminokoe  
       2023-07-20 05:57:36 +08:00 via iPhone
    还是润了舒服,不用配置这些乱七八糟的东西,什么都可以直连
    coderdusk
        2
    coderdusk  
       2023-07-20 08:35:01 +08:00
    刚搜了一下,port 好像传的数字类型而不是字符串吧
    monster1priest
        3
    monster1priest  
    OP
       2023-07-20 14:18:37 +08:00
    @coderdusk 试过了,和这个没什么关系
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1259 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:05 · PVG 02:05 · LAX 10:05 · JFK 13:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.