Linux cURL cookie 语法(来自 bash CLI,不是 cookie 文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9223319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:30:13  来源:igfitidea点击:

cURL cookie syntax (from bash CLI, not cookie file)

linuxbashhttpcurl

提问by JT Gray

I have a web server that returns to me the same cookie value which I sent it in my request. For this, I have been using the --cookie file successfully (minus a minor speed bump originating with a tabs to spaces issue in the cookie file).

我有一个 Web 服务器,它会返回与我在请求中发送的相同 cookie 值。为此,我一直在成功地使用 --cookie 文件(减去由 cookie 文件中的制表符到空格问题引起的轻微减速)。

Unfortunately, if I run the same command but with the cookie name and value in the command rather than in the cookie file, my server does not respond with the desired results.

不幸的是,如果我运行相同的命令,但在命令中而不是在 cookie 文件中使用 cookie 名称和值,我的服务器不会以所需的结果响应。

According to the cURL docs, something like this should work:

根据 cURL 文档,这样的事情应该有效:

curl --cookie 'cookiename=cookievalue' --cookie-jar - http://my.site.com/page/with/cookies-v

curl --cookie 'cookiename=cookievalue' --cookie-jar - http://my.site.com/page/with/cookies-v

But it does not. However, if I use the tab-delimited cookie file instead of the parameters in the command line, it works successfully. What's more is that I have tried pretty much every permutation of these cookie params at the CLI to no avail: tab-delimited, name=cookiename;value=cookievalue(etc), with commas, with the path, as --header, etc etc.

但事实并非如此。但是,如果我在命令行中使用制表符分隔的 cookie 文件而不是参数,它会成功运行。更重要的是,我在 CLI 中尝试了几乎所有这些 cookie 参数的排列都无济于事:制表符分隔,名称=cookiename;value=cookievalue(etc),带逗号,带路径,如 --header 等等等。

When I compare the outbound requests side-by-side, I see no apparent difference between the successful and unsuccessful requests, but the response doesn't return the intended results if I pass cookies in the command line without using the cookie file. What important but subtle nuance am I overlooking here?

当我并排比较出站请求时,我发现成功和不成功的请求之间没有明显区别,但是如果我在命令行中传递 cookie 而不使用 cookie 文件,则响应不会返回预期的结果。我在这里忽略了哪些重要但微妙的细微差别?

回答by dldnh

I was curious to see whether this would be a problem on my server and what I would need to solve it.

我很好奇这是否会成为我服务器上的问题以及我需要解决的问题。

First thing was I tested using a browser, to study the baseline behavior.

首先是我使用浏览器进行测试,以研究基线行为。

To start, I connected to make sure no cookies were set yet:

首先,我连接以确保尚未设置 cookie:

http://hostname/test/testcookie.php

Cookie: array(0) {
}

Then I connected to my script to set a cookie.

然后我连接到我的脚本来设置一个 cookie。

http://hostname/test/setcookie.php

Cookie set.

And then I checked to make sure the browser was sending that new cookie:

然后我检查以确保浏览器正在发送新的 cookie:

http://hostname/test/testcookie.php

Cookie: array(1) {
  ["cookiename"]=>
  string(11) "cookievalue"
}

Once I had verified the browser/server behavior, it was time to make things happen from the command line using curl.

一旦我验证了浏览器/服务器的行为,就可以使用curl.

First we'll see what happens when we don't try and send a cookie to the server:

首先,我们将看看当我们不尝试向服务器发送 cookie 时会发生什么:

$ curl -s "http://hostname/test/testcookie.php"

And here is the result:

结果如下:

Cookie: array(0) {
}

Next we'll see what happens when we do send a cookie, using --cookieas mentioned in the question:

接下来,我们将看看当我们发送 cookie 时会发生什么,使用--cookie问题中提到的:

$ curl -s --cookie "cookiename=cookievalue" "http://hostname/test/testcookie.php"

Cookie: array(1) {
  ["cookiename"]=>
  string(11) "cookievalue"
}

And this demonstrates that the --cookieparameter doeswork and perhaps the OP has other issues going on that might need some debugging.

这表明该--cookie参数确实有效,而且 OP 可能还有其他问题可能需要进行一些调试。

So now, just for fun, let's see what happens when we use a cookie file and call the cookie setting script:

现在,为了好玩,让我们看看当我们使用 cookie 文件并调用 cookie 设置脚本时会发生什么:

$ curl -s --cookie-jar /tmp/cookiefile "http://hostname/test/setcookie.php"

Cookie set.

Here's what that cookie looks like in the file:

这是该 cookie 在文件中的样子:

hostnametabFALSEtab/test/tabFALSEtab1333484771tabcookienametabcookievalue

主机名tabFALSE tab/test/ tabFALSE tab1333484771 tabcookiename tabcookievalue

And now we'll call the cookie tester and use the cookie file this time.

现在我们将调用 cookie 测试器并使用 cookie 文件。

$ curl -s --cookie /tmp/cookiefile "http://hostname/test/testcookie.php"

Cookie: array(1) {
  ["cookiename"]=>
  string(11) "cookievalue"
}

I hope this helps with debugging cookie issues that anybody might have.

我希望这有助于调试任何人可能遇到的 cookie 问题。



And here are the files I used on my server, in my /testpath.

这是我在我的服务器上使用的文件,在我的/test路径中。

Here is /www/test/testcookie.php:

这是/www/test/testcookie.php:

<?php
header("Content-type: text/plain");
echo "Cookie: ";
var_dump($_COOKIE);
?>

And here is /www/test/setcookie.php:

这是/www/test/setcookie.php:

<?php
header("Content-type: text/plain");
setcookie("cookiename", "cookievalue", time()+86400 );
?>
Cookie set.