bash curl - 如何在登录后获取 cookie 以发送 curl 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45755084/
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
curl - How to get a cookie from after login to send in a curl command?
提问by kumoyadori
I want to get access to a json page that contains data.
Say, it's there under http://example.com/home/apples.json
It's protected, so a login page is shown.
If I were to visit it manually I first go to http://example.com/
There I get a login menu and after login as say user "test" with password "test" visiting http://example.com/home/apples.json again
will show me the json data.
我想访问包含数据的 json 页面。比如说,它在http://example.com/home/apples.json
它的保护之下,所以会显示一个登录页面。如果我要手动访问它,我首先去http://example.com/
那里我得到一个登录菜单,登录后说用户“test”和密码“test”访问http://example.com/home/apples.json again
将向我显示json数据。
If I do a
如果我做一个
curl -u test:test http://example.com/home/apples.json -v
I end up at the login page. (Despite the authentication.)
我最终在登录页面。(尽管经过身份验证。)
If I visit http://example.com/and manually login I will get a cookie after login. Say this cookie is called ltoken and gets the value "dj8f". It will only show up after a successfull login. When I look it up via the browser and copy the cookie data and attach it to the curl command:
如果我访问http://example.com/并手动登录,我会在登录后得到一个 cookie。假设这个 cookie 被称为 ltoken 并获得值“dj8f”。只有在成功登录后才会显示。当我通过浏览器查找并复制 cookie 数据并将其附加到 curl 命令时:
curl -b "ltoken="dj8f" http://example.com/home/apples.json -v
it will work.
它会起作用。
How can I get the cookie data from after login without doing it manually? Is it possible via bash shell scripting means? If yes how? Alternatively how would it be done in a groovy script?
如何在不手动执行的情况下从登录后获取 cookie 数据?是否可以通过 bash shell 脚本编写方式?如果是如何?或者,如何在 groovy 脚本中完成?
回答by sauerburger
You have to perform a proper login with curl first. In order to do this, navigate to the login page and have a look at the source code. The login form should look something like this.
您必须首先使用 curl 执行正确的登录。为此,请导航到登录页面并查看源代码。登录表单应如下所示。
<form action="http://example.com/login/target" method="POST">
<input type="text" name="username" />
<input type="password" name="passphrase" />
<input type="submit" value="Log in" />
</form>
(I assume that there are no hidden input fields. Hidden input fields are often used to protect against request forgery, which is basically what we are going to do.)
(我假设没有隐藏的输入字段。隐藏的输入字段通常用于防止请求伪造,这基本上是我们要做的。)
From this snipped you have to extract the target of the login request (in this example http://example.com/login/target
) the names of the HTML input fields (here username
and passphrase
). To perform the login process, you should send the login information to the target, for example by executing
从这个片段中,您必须提取登录请求的目标(在本例中http://example.com/login/target
)HTML 输入字段的名称(此处username
和passphrase
)。要执行登录过程,您应该将登录信息发送到目标,例如通过执行
curl --cookie-jar cookies.txt --form passphrase=test --form username=test http://example.com/login/target
(Please note, that is generally not advisable to type your password on the command line in this way. Your password is probably stored in a command history and could be stolen.)
(请注意,通常不建议以这种方式在命令行中输入您的密码。您的密码可能存储在命令历史记录中并且可能被盗。)
The --cookie-jar
option tells curl to store all cookies in a file. If the login succeeds and the server sets session cookies, curl will save them in this text file.
该--cookie-jar
选项告诉 curl 将所有 cookie 存储在一个文件中。如果登录成功并且服务器设置了会话 cookie,curl 会将它们保存在此文本文件中。
After a successful login, you should be able to access the json-file, if the request contains the cookies from the cookie-jar. This can be achieved with the -b
argument. If -b
does not contain the =
character, curl will use the contents of the file as cookies.
成功登录后,如果请求包含来自 cookie-jar 的 cookie,您应该能够访问 json 文件。这可以通过-b
参数来实现 。如果-b
不包含该=
字符,curl 将使用文件内容作为 cookie。
curl -b cookies.txt http://example.com/home/apples.json -v