发送自定义User-Agent字符串以及我的标头(提取) [英] Sending a custom User-Agent string along with my headers (fetch)

查看:205
本文介绍了发送自定义User-Agent字符串以及我的标头(提取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在React中使用fetch API,并且从JSON端点提取了一些数据.

作为请求的一部分,我想发送一个自定义的User-Agent字符串.当前,当我检查请求时,UA字符串为:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

由于我在每个请求中都添加了标头,所以我想将User-Agent附加到标头对象中,就像它在位置在线:

fetch(url, {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'User-Agent': 'MY-UA-STRING' // <---
})

但这不起作用.我感觉这是由于获取api中的错误所致,据报道此处此处解决方案

经过一些测试,Chrome浏览器确实确实存在User-Agent标头的错误.

这很可能是由于不久前(2015年中)User-Agent标头出现在了禁止标头列表中.

由于此特定标头最近已从不允许的标头列表中删除,因此Firefox(版本43)将允许您在fetch调用中更改User-Agent,但Chrome不会.

这是 Firefox错误标题创建标题界面,并将它们包含在选项对象中headers键下

let headers = new Headers({
    "Accept"       : "application/json",
    "Content-Type" : "application/json",
    "User-Agent"   : "MY-UA-STRING"
});

fetch(url, {
    method  : 'GET', 
    headers : headers 
    // ... etc
}).then( ...

I'm using the fetch API in React, and I'm pulling down some data from a JSON endpoint.

As part of my requests, I want to send a custom User-Agent string. Currently, when I inspect my requests, the UA string is:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

Since I'm pasing in headers with every request, I figured I'd just append User-Agent to the headers object, like it says in various places online:

fetch(url, {
    Accept: 'application/json',
    'Content-Type': 'application/json',
    'User-Agent': 'MY-UA-STRING' // <---
})

But this doesn't work. I have a feeling it's becuase of a bug in the fetch api, as reported here and here and here.

Anyone have a work around on how to pass UA as part of the headers using fetch?

解决方案

After some testing, Chrome does indeed have a bug with the User-Agent header.

This is most likely due to the fact that the User-Agent header was on the list of disallowed headers not too long ago (mid 2015).

As this particular header was recently removed from the list of disallowed headers, Firefox (from version 43) will let you change the User-Agent in a fetch call, but Chrome won't.

Here's the Firefox bug, and the Chromium bug

The reason it was disallowed in the first place, was that there's really no good reason to use the User-Agent header to send arbitrary data, it should be used to send the actual User-Agent, and in-browser requests like Fetch of XMLHttpRequest should really have no good reason to spoof the User Agent anyway.

When the bug will be fixed in Chrome is anyones guess, but it is indeed a bug as the list of disallowed headers no longer lists the User-Agent header, and it should change when specified in the options object of Fetch.

As a sidenote, you should generally be creating the headers using the Headers Interface, and include them in the options objects under the headers key

let headers = new Headers({
    "Accept"       : "application/json",
    "Content-Type" : "application/json",
    "User-Agent"   : "MY-UA-STRING"
});

fetch(url, {
    method  : 'GET', 
    headers : headers 
    // ... etc
}).then( ...

这篇关于发送自定义User-Agent字符串以及我的标头(提取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆