使用 Ruby Curb gem 和 API 代币
Using Ruby Curb gem with API token
全新 Ruby。我正在参考 Curb 文档并尝试在使用安全令牌的 curl 中提交请求。以下是我的 header 的 3 个部分以及我的传统要求:
- My-Security-Token
- 1234567890
- website.com
我将如何构造 Curb 以提交如上所示的名称、价值和评论?
我从什么开始:
c = Curl::Easy.new("http://website.com") do |curl|
curl.headers["Name"] = "My-Security-Token"
curl.headers["Value"] = "1234567890"
curl.headers["Comment"] = "website.com"
curl.verbose = true
end
c.perform
Here are the 3 parts to my header with my traditional request
- My-Security-Token
- 1234567890
- website.com
Headers由name/value对组成,所以你的描述没有任何意义。
看起来你的 header 名字应该是:
My-Security-Token
它的值应该是:
1234567890
要发出 GET request
(您没有提及您发出的请求类型)并指定 header,您可以这样做:
require 'curb'
http = Curl.get("http://website.com/") do |http|
http.headers["My-Security-Token"] = "1234567890"
end
puts http.body_str[0..249] #Output the first 250 characters of the response
如果你想使用详细模式,它会显示实际的请求和响应,你可以这样做:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["My-Security-Token"] = "1234567890"
curl.verbose = true
end
c.perform #Outputs the request and the response
puts c.body_str[0..249] #body_str => the body of the response
输出:
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.147... * Connected to www.google.com (74.125.28.147) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
My-Security-Token: 1234567890 #***HERE IS YOUR CUSTOM HEADER****
< HTTP/1.1 200 OK
< Date: Tue, 03 Mar 2015 00:14:42 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< Set-Cookie: PREF=ID=479a8fa626097193:FF=0:TM=1425341682:LM=1425341682:S=5kflGPOAzEx-uMWb; expires=Thu, 02-Mar-2017 00:14:42 GMT; path=/; domain=.google.com
< Set-Cookie: NID=67=Eim3D__PFGbpSWdcIH9IPhkuDEVMFjN4ShU9gA6Z_rMryMoI6nv--sIjk_E00_EpMfSe3RkPO5dYjV7yGTXT3oMLX-t7FsrKSJmF7-OffAuLKrr5KfV1IZzL9yaJQKiB; expires=Wed, 02-Sep-2015 00:14:42 GMT; path=/; domain=.google.com; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alternate-Protocol: 80:quic,p=0.08
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
* Expire cleared
* Connection #0 to host www.google.com left intact
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many specia
如果您确实希望自定义 header 为:
Name: My-Security-Token
Value: 1234567890
Comment: website.com
...那么你可以这样做:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["Name"] = "My-Security-Token"
curl.headers["Value"] = "1234567890"
curl.headers["Comment"] = "website.com"
curl.verbose = true
end
c.perform
puts c.body_str[0..249]
...这将产生如下输出:
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.105... * Connected to www.google.com (74.125.28.105) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
Name: My-Security-Token #HERE ARE YOUR HEADERS
Value: 1234567890 #HERE
Comment: website.com #HERE
...
...
如果上面没有满足您的要求,那么与其描述您的请求,不如发布您的 实际 "traditional" 请求怎么样?如果您不知道该怎么做,请搜索 google.
How would I structure Curb to submit Name, Value and Comment as seen above?
再想想,Name、Value 和 Comment 似乎是单个 http 的组成部分 header:
Comments can be included in some HTTP header fields by surrounding
the comment text with parentheses. Comments are only allowed in
fields containing "comment" as part of their field value definition.
In all other fields, parentheses are considered part of the field
value.
如果是这种情况,您可以像这样创建自定义 header:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["My-Security-Token"] = "1234567890 (website.com)"
curl.verbose = true
end
c.perform #Outputs the request and the response
puts c.body_str[0..249] #body_str => the body of the response
--output:--
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.104... * Connected to www.google.com (74.125.28.104) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
My-Security-Token: 1234567890 (website.com) #***YOUR CUSTOM HEADER****
...
...
全新 Ruby。我正在参考 Curb 文档并尝试在使用安全令牌的 curl 中提交请求。以下是我的 header 的 3 个部分以及我的传统要求:
- My-Security-Token
- 1234567890
- website.com
我将如何构造 Curb 以提交如上所示的名称、价值和评论?
我从什么开始:
c = Curl::Easy.new("http://website.com") do |curl|
curl.headers["Name"] = "My-Security-Token"
curl.headers["Value"] = "1234567890"
curl.headers["Comment"] = "website.com"
curl.verbose = true
end
c.perform
Here are the 3 parts to my header with my traditional request
- My-Security-Token
- 1234567890
- website.com
Headers由name/value对组成,所以你的描述没有任何意义。
看起来你的 header 名字应该是:
My-Security-Token
它的值应该是:
1234567890
要发出 GET request
(您没有提及您发出的请求类型)并指定 header,您可以这样做:
require 'curb'
http = Curl.get("http://website.com/") do |http|
http.headers["My-Security-Token"] = "1234567890"
end
puts http.body_str[0..249] #Output the first 250 characters of the response
如果你想使用详细模式,它会显示实际的请求和响应,你可以这样做:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["My-Security-Token"] = "1234567890"
curl.verbose = true
end
c.perform #Outputs the request and the response
puts c.body_str[0..249] #body_str => the body of the response
输出:
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.147... * Connected to www.google.com (74.125.28.147) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
My-Security-Token: 1234567890 #***HERE IS YOUR CUSTOM HEADER****
< HTTP/1.1 200 OK
< Date: Tue, 03 Mar 2015 00:14:42 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< Set-Cookie: PREF=ID=479a8fa626097193:FF=0:TM=1425341682:LM=1425341682:S=5kflGPOAzEx-uMWb; expires=Thu, 02-Mar-2017 00:14:42 GMT; path=/; domain=.google.com
< Set-Cookie: NID=67=Eim3D__PFGbpSWdcIH9IPhkuDEVMFjN4ShU9gA6Z_rMryMoI6nv--sIjk_E00_EpMfSe3RkPO5dYjV7yGTXT3oMLX-t7FsrKSJmF7-OffAuLKrr5KfV1IZzL9yaJQKiB; expires=Wed, 02-Sep-2015 00:14:42 GMT; path=/; domain=.google.com; HttpOnly
< P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Alternate-Protocol: 80:quic,p=0.08
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
* Expire cleared
* Connection #0 to host www.google.com left intact
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world's information, including webpages, images, videos and more. Google has many specia
如果您确实希望自定义 header 为:
Name: My-Security-Token
Value: 1234567890
Comment: website.com
...那么你可以这样做:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["Name"] = "My-Security-Token"
curl.headers["Value"] = "1234567890"
curl.headers["Comment"] = "website.com"
curl.verbose = true
end
c.perform
puts c.body_str[0..249]
...这将产生如下输出:
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.105... * Connected to www.google.com (74.125.28.105) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
Name: My-Security-Token #HERE ARE YOUR HEADERS
Value: 1234567890 #HERE
Comment: website.com #HERE
...
...
如果上面没有满足您的要求,那么与其描述您的请求,不如发布您的 实际 "traditional" 请求怎么样?如果您不知道该怎么做,请搜索 google.
How would I structure Curb to submit Name, Value and Comment as seen above?
再想想,Name、Value 和 Comment 似乎是单个 http 的组成部分 header:
Comments can be included in some HTTP header fields by surrounding
the comment text with parentheses. Comments are only allowed in
fields containing "comment" as part of their field value definition.
In all other fields, parentheses are considered part of the field
value.
如果是这种情况,您可以像这样创建自定义 header:
require 'curb'
c = Curl::Easy.new("http://www.google.com/") do |curl|
curl.headers["My-Security-Token"] = "1234567890 (website.com)"
curl.verbose = true
end
c.perform #Outputs the request and the response
puts c.body_str[0..249] #body_str => the body of the response
--output:--
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.28.104... * Connected to www.google.com (74.125.28.104) port 80 (#0)
> GET / HTTP/1.1
Host: www.google.com
Accept: */*
My-Security-Token: 1234567890 (website.com) #***YOUR CUSTOM HEADER****
...
...