Javascript 以冒号开头的HTTP/2请求和头
你好,亲爱的苏社区 我的问题折磨了我好几个月,没有解决办法 我试图在HTTP/2端点发出请求,该端点使用一些以冒号开头的头。例如:Javascript 以冒号开头的HTTP/2请求和头,javascript,php,python,request,Javascript,Php,Python,Request,你好,亲爱的苏社区 我的问题折磨了我好几个月,没有解决办法 我试图在HTTP/2端点发出请求,该端点使用一些以冒号开头的头。例如: :method: "POST" 我尝试过python(hyper、requests)、php(guzzle)和js(fetch)。 据推测,我已经使用js实现了所需的结果,但是CORS策略返回了一个“不透明”的结果 任何帮助都会很好 JS结果假定正确“不透明” Python结果500或404 context = tls.init_context() co
:method: "POST"
我尝试过python(hyper、requests)、php(guzzle)和js(fetch)。
据推测,我已经使用js实现了所需的结果,但是CORS策略返回了一个“不透明”的结果
任何帮助都会很好
JS结果假定正确“不透明”
Python结果500或404
context = tls.init_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
with HTTP20Connection('www.example.com', port=443, ssl_context=context) as c:
headers = {
":authority": "www.example.com",
":method": "GET",
":path": "/users/sign_in",
":scheme": "https",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9,el;q=0.8",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
}
c.request('GET', 'www.example.com/users/sign_in', headers=headers)
PHP结果500或卷曲错误55
$jar = new CookieJar;
$client = new Client([
// Base URI is used with relative requests
'cookies' => $jar,
'version' => 2.0,
'debug' => fopen('php://stderr', 'w'),
]);
$client->request('GET', 'https://www.example.com/users/sign_in');
$response = $client->request('POST', 'https://www.example.com/users/sign_in', [
'headers' => [
":authority"=> "www.example.com",
":method"=> "POST",
":path"=> "/users/sign_in",
":scheme"=> "https",
"accept"=> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"accept-encoding"=> "gzip, deflate, br",
"accept-language"=> "en-US,en;q=0.9,el;q=0.8",
"cache-control"=> "max-age=0",
"content-length"=> "616",
"content-type"=> "application/x-www-form-urlencoded",
"origin"=> "https://www.example.com",
"referer"=> "https://www.example.com/users/sign_in",
"sec-fetch-mode"=> "navigate",
"sec-fetch-site"=> "same-origin",
"sec-fetch-user"=> "?1",
"upgrade-insecure-requests"=> "1",
"user-agent"=> "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
],
'form_params' => [ ... ],
'version' => 2.0,
'allow_redirects' => true
]);
这些是HTTP/2伪头,应用于HTTP/2流中的请求和响应。HTTP/2创建从每个不同的源端到服务器的单个持久连接。该连接将多个请求和响应发送到端点或从端点发送;这些被解析为“帧”并作为“流”传输。HTTP/2可以同时交错来自多个请求和响应流的帧,以获得巨大的性能优势 伪头应用于流;一组不同的头应用于连接本身。为requests::method、:scheme、:authority和:path定义了四个伪头。不允许有其他人。这4个必须包含在每个请求标头块中,并且必须位于任何其他标头之前: “所有伪标题字段必须出现在标题块中,然后 常规标头字段。任何包含 出现在标题块中的常规标题之后的伪标题字段 必须将标题字段视为格式错误(第8.1.2.6节)。”
我不熟悉“fetch”如何实现头,但在上面的代码中,您似乎在头块之外有伪头,它们被放在末尾。可能是抓取出错。我认为标题字段不能以
:
开头。这是标题名称和值之间的分隔符。看起来您试图使用的标题只是URL中的重复字段。你为什么认为这是必要的?谢谢你们两位的回复。正如您在devtools的屏幕截图中所看到的,请求中包含了标题。如果没有标题,请求将返回404或500响应。这些不是真正的标题,只是开发人员工具显示HTTP请求行详细信息的方式。开头的冒号表示它们不是真的。@Barmar那么为什么在它们不存在的时候使用JS脚本,我会得到500个响应,当它们存在的时候,请求会顺利通过?
$jar = new CookieJar;
$client = new Client([
// Base URI is used with relative requests
'cookies' => $jar,
'version' => 2.0,
'debug' => fopen('php://stderr', 'w'),
]);
$client->request('GET', 'https://www.example.com/users/sign_in');
$response = $client->request('POST', 'https://www.example.com/users/sign_in', [
'headers' => [
":authority"=> "www.example.com",
":method"=> "POST",
":path"=> "/users/sign_in",
":scheme"=> "https",
"accept"=> "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"accept-encoding"=> "gzip, deflate, br",
"accept-language"=> "en-US,en;q=0.9,el;q=0.8",
"cache-control"=> "max-age=0",
"content-length"=> "616",
"content-type"=> "application/x-www-form-urlencoded",
"origin"=> "https://www.example.com",
"referer"=> "https://www.example.com/users/sign_in",
"sec-fetch-mode"=> "navigate",
"sec-fetch-site"=> "same-origin",
"sec-fetch-user"=> "?1",
"upgrade-insecure-requests"=> "1",
"user-agent"=> "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
],
'form_params' => [ ... ],
'version' => 2.0,
'allow_redirects' => true
]);