如何使用curl 将cookie 传递给Sinatra 应用程序?
我正在使用本教程中标题为“稍大一点的示例”的示例中的代码 http://rubylearning.com/blog/2009/09/30/cookie-based-sessions-in-sinatra/ 了解如何将 cookie 发送到 Sinatra 应用程序但我不知道如何正确设置这些值
当我在应用程序中将名称设置为“brandon”时,它会创建一个值为 BAh7BiIJdXNlciIMYnJhbmRvbg%3D%3D%0A
的 cookie的 url 编码 (http://ostermiller.org/calc/encode.html) value BAh7BiIJdXNlciIMYnJhbmRvbg==
使用该值,我可以正确地将 cookie 发送到应用程序,
curl -b "rack.session=BAh7BiIJdXNlciIMYnJhbmRvbg==" localhost:9393
我很确定该值是自文档以来会话的 ruby 哈希值的 base64 编码(http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html )说
会话是一个 Ruby 哈希,存储为 base64 编码的编组数据集,设置为:key(默认值:rack.session)。
我认为这意味着我所要做的就是进行 base64 编码 {"user"=>"brandon"}
并在curl 命令中使用它。不幸的是,这创建了与 BAh7BiIJdXNlciIMYnJhbmRvbg==
不同的值。接下来,我尝试获取 Base64 编码值并在各种在线 Base64 解码器上对其进行解码,但这会产生奇怪的字符(方框符号等),因此我不知道如何重新创建该值以对其进行编码。
所以我的问题是你是否知道我需要什么字符/格式来获得正确的base64编码和/或你是否知道使用curl传递值的另一种方法,以便它将注册为Sinatra应用程序的正确cookie?
I'm using the code from the example titled "A Slightly Bigger Example" from this tutorial http://rubylearning.com/blog/2009/09/30/cookie-based-sessions-in-sinatra/ to figure out how to send a cookie to a Sinatra application but I can't figure out how to set the values correctly
When I set the name to be "brandon" in the application it creates a cookie with a value of BAh7BiIJdXNlciIMYnJhbmRvbg%3D%3D%0A
which is a url encoding (http://ostermiller.org/calc/encode.html) of the value BAh7BiIJdXNlciIMYnJhbmRvbg==
Using that value I can send a cookie to the app correctly
curl -b "rack.session=BAh7BiIJdXNlciIMYnJhbmRvbg==" localhost:9393
I'm pretty sure that value is a base64 encoding of the ruby hash for the session since the docs (http://rack.rubyforge.org/doc/classes/Rack/Session/Cookie.html) say
The session is a Ruby Hash stored as base64 encoded marshalled data set to :key (default: rack.session).
I thought that meant all I had to do was base64 encode {"user"=>"brandon"}
and use it in the curl command. Unfortunately that creates a different value than BAh7BiIJdXNlciIMYnJhbmRvbg==
. Next I tried taking the base64 encoded value and decoding it at various base64 decoders online but that results in strange characters (a box symbol and others) so I don't know how to recreate the value to even encode it.
So my question is do you know what characters/format I need to get the proper base64 encoding and/or do you know of another way to pass a value using curl such that it will register as a proper cookie for a Sinatra app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的意思是在登录页面上以表单形式输入用户名和密码,则 cURL 可以“提交”该表单,例如:
curl -d "username=miniape&password=SeCrEt" http://whatever.com/login
如果你想存储返回的 cookie,你可以通过指定 cookie 文件来实现:
curl -c cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login
并使用这些您执行的后续请求中的 cookie:
curl -b cookies.txt -d "username=miniape&password=SeCrEt" http:// whatever.com/login
或者如果您想同时发送和接收 cookie,请同时执行以下操作:
curl -b cookies.txt -c cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login
在元过滤器上找到了这个
f you mean the username and password are entered in a form on a login page, then cURL can "submit" that form like:
curl -d "username=miniape&password=SeCrEt" http://whatever.com/login
and if you want to store the cookie that comes back you do so by specifying a cookie file:
curl -c cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login
and to use those cookie in later requests you do:
curl -b cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login
or do both if you want to both send and receive cookies:
curl -b cookies.txt -c cookies.txt -d "username=miniape&password=SeCrEt" http://whatever.com/login
found this on metafilter