Axios 正在替换用户代理字符串

问题描述

这个调用使用 curl 可以正常工作,但不能从使用 axios 的节点调用。看起来 axios 没有从标头正确发送用户代理。如果我没有在标题中传递用户代理,它会以“axios/0.21.1”的形式返回错误

我已经为不同的浏览器尝试了完整的用户代理字符串并将其传递到标头中,但都没有奏效。

curl https://search.mobile.walmart.com/v1/products-by-code/UPC/405760947563?storeId=1 -H "User-Agent:Mozilla/5.0"

这是节点中的代码

const axios = require('axios');

const checkItem = (upc,store) => {
    let url = `https://search.mobile.walmart.com/v1/products-by-code/UPC/${upc}`;
    return axios.get(url,{
        headers: {
            'User-Agent': 'Mozilla/5.0 (iPhone; cpu iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML,like Gecko) Version/12.0 Mobile/15E148 Safari/604.1'
        },params: { storeId: store }
    }).then(resp => {
        console.log(resp);
    }).catch(err => {
        console.log(err);
    });
}

checkItem(405760947563,5146);

这是错误

Error: Request Failed with status code 412
    at createError (C:\testproj\node_modules\axios\lib\core\createError.js:16:15)
    at settle (C:\testproj\node_modules\axios\lib\core\settle.js:17:12)
    at IncomingMessage.handleStreamEnd (C:\testproj\node_modules\axios\lib\adapters\http.js:260:11)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1221:12)
    at processticksAndRejections (internal/process/task_queues.js:84:21) {
  config: {
    url: 'https://search.mobile.walmart.com/v1/products-by-code/UPC/405760947563',method: 'get',headers: {
      Accept: 'application/json,text/plain,*/*','User-Agent': 'Mozilla/5.0 (iPhone; cpu iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML,like Gecko) Version/12.0 Mobile/15E148 Safari/604.1'
    },params: { storeId: 5146 },transformRequest: [ [Function: transformRequest] ],transformResponse: [ [Function: transformResponse] ],timeout: 0,adapter: [Function: httpAdapter],xsrfCookieName: 'XSRF-TOKEN',xsrfheaderName: 'X-XSRF-TOKEN',maxContentLength: -1,maxBodyLength: -1,validateStatus: [Function: validateStatus],data: undefined
  },request: ClientRequest {
    _events: [Object: null prototype] {
      socket: [Function],abort: [Function],aborted: [Function],connect: [Function],error: [Function],timeout: [Function],prefinish: [Function: requestOnPrefinish]
    },_eventsCount: 7,_maxListeners: undefined,outputData: [],outputSize: 0,writable: true,_last: true,chunkedEncoding: false,shouldKeepAlive: false,useChunkedEncodingByDefault: false,sendDate: false,_removedConnection: false,_removedContLen: false,_removedTE: false,_contentLength: 0,_hasBody: true,_trailer: '',finished: true,_headerSent: true,socket: TLSSocket {
      _tlsOptions: [Object],_secureEstablished: true,_securePending: false,_newSessionPending: false,_controlReleased: true,secureConnecting: true,_SNICallback: null,servername: 'search.mobile.walmart.com',alpnProtocol: false,authorized: true,authorizationError: null,encrypted: true,_events: [Object: null prototype],_eventsCount: 10,connecting: false,_hadError: false,_parent: null,_host: 'search.mobile.walmart.com',_readableState: [ReadableState],readable: true,_writableState: [WritableState],writable: false,allowHalfOpen: false,_sockname: null,_pendingData: null,_pendingEncoding: '',server: undefined,_server: null,ssl: [TLSWrap],_requestCert: true,_rejectUnauthorized: true,parser: null,_httpMessage: [Circular],[Symbol(res)]: [TLSWrap],[Symbol(verified)]: true,[Symbol(pendingSession)]: null,[Symbol(asyncId)]: 5,[Symbol(kHandle)]: [TLSWrap],[Symbol(kSetNoDelay)]: false,[Symbol(lastWriteQueueSize)]: 0,[Symbol(timeout)]: null,[Symbol(kBuffer)]: null,[Symbol(kBufferCb)]: null,[Symbol(kBufferGen)]: null,[Symbol(kCapture)]: false,[Symbol(kBytesRead)]: 0,[Symbol(kBytesWritten)]: 0,[Symbol(connect-options)]: [Object]
    },connection: TLSSocket {
      _tlsOptions: [Object],_header: 'GET /v1/products-by-code/UPC/405760947563?storeId=5146 HTTP/1.1\r\n' +
      'Accept: application/json,*/*\r\n' +
      'User-Agent: Mozilla/5.0 (iPhone; cpu iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML,like Gecko) Version/12.0 Mobile/15E148 Safari/604.1\r\n' +
      'Host: search.mobile.walmart.com\r\n' +
      'Connection: close\r\n' +
      '\r\n',_onPendingData: [Function: noopPendingOutput],agent: Agent {
      _events: [Object: null prototype],_eventsCount: 2,defaultPort: 443,protocol: 'https:',options: [Object],requests: {},sockets: [Object],freeSockets: {},keepAliveMsecs: 1000,keepAlive: false,maxSockets: Infinity,maxFreeSockets: 256,maxCachedSessions: 100,_sessionCache: [Object],[Symbol(kCapture)]: false
    },socketPath: undefined,method: 'GET',insecureHTTPParser: undefined,path: '/v1/products-by-code/UPC/405760947563?storeId=5146',_ended: true,res: IncomingMessage {
      _readableState: [ReadableState],readable: false,_eventsCount: 3,socket: [TLSSocket],connection: [TLSSocket],httpVersionMajor: 1,httpVersionMinor: 1,httpVersion: '1.1',complete: true,headers: [Object],rawHeaders: [Array],trailers: {},rawTrailers: [],aborted: false,upgrade: false,url: '',method: null,statusCode: 412,statusMessage: 'Precondition Failed',client: [TLSSocket],_consuming: false,_dumped: false,req: [Circular],responseUrl: 'https://search.mobile.walmart.com/v1/products-by-code/UPC/405760947563?storeId=5146',redirects: [],timeoutCb: null,upgradeOrConnect: false,maxHeadersCount: null,reusedSocket: false,_redirectable: Writable {
      _writableState: [WritableState],_options: [Object],_ending: true,_redirectCount: 0,_redirects: [],_requestBodyLength: 0,_requestBodyBuffers: [],_onNativeResponse: [Function],_currentRequest: [Circular],_currentUrl: 'https://search.mobile.walmart.com/v1/products-by-code/UPC/405760947563?storeId=5146',[Symbol(kNeedDrain)]: false,[Symbol(corked)]: 0,[Symbol(kOutHeaders)]: [Object: null prototype] {
      accept: [Array],'user-agent': [Array],host: [Array]
    }
  },response: {
    status: 412,statusText: 'Precondition Failed',headers: {
      'accept-ranges': 'bytes','content-length': '296','content-type': 'application/json; charset=UTF-8',pragma: 'no-cache','cache-control': 'no-cache,no-store',expires: 'Wed,03 Feb 2021 21:22:23 GMT',date: 'Wed,connection: 'close','set-cookie': [Array],vary: 'accept-encoding'
    },config: {
      url: 'https://search.mobile.walmart.com/v1/products-by-code/UPC/405760947563',params: [Object],transformRequest: [Array],transformResponse: [Array],data: undefined
    },request: ClientRequest {
      _events: [Object: null prototype],_header: 'GET /v1/products-by-code/UPC/405760947563?storeId=5146 HTTP/1.1\r\n' +
        'Accept: application/json,*/*\r\n' +
        'User-Agent: Mozilla/5.0 (iPhone; cpu iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML,like Gecko) Version/12.0 Mobile/15E148 Safari/604.1\r\n' +
        'Host: search.mobile.walmart.com\r\n' +
        'Connection: close\r\n' +
        '\r\n',agent: [Agent],res: [IncomingMessage],_redirectable: [Writable],[Symbol(kOutHeaders)]: [Object: null prototype]
    },data: {
      appId: 'PXUArm9B04',jsClientSrc: '/px/PXUArm9B04/init.js',firstPartyEnabled: true,vid: '',uuid: 'e80bce70-6665-11eb-b34b-19359e0aac5a',hostUrl: '/px/PXUArm9B04/xhr',blockScript: '/px/PXUArm9B04/captcha/captcha.js?a=c&m=0&u=e80bce70-6665-11eb-b34b-19359e0aac5a&v=&g=b'
    }
  },isAxiosError: true,toJSON: [Function: toJSON]
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)