关于使用 ruby 代码的 http POST api 调用的问题
Issue regarding http POST api call using ruby code
我将访问 Riskscreen api 以验证用户身份。为了测试 api,我编写了一个 ruby 代码片段来进行示例 POST 调用,以获取我从 Riskscreen api.
获得的令牌数量
我的代码是:
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
@toSend = {}.to_json
uri = URI.parse("https://api.riskscreen.com/api/v1/user/tokens")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {'api-key': 'my api key','Content-Type': 'application/json', 'Accept': 'application/json'}
req = Net::HTTP::Post.new(uri.path, header)
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "------------"
puts "Response #{res.code} #{res.message}: #{res.body}"
但我收到以下错误:
Response 400 Bad Request
如果我将 header 行更改为
header = {'api-key'=> 'my-api-key','Content-Type'=> 'application/json', 'Accept'=> 'application/json'}
然后我收到这个错误:
Response 401 Unauthorized
坚持一段时间。请帮我解决这个问题。
你能试试删除吗:
req.body = "[ #{@toSend} ]"
并替换为:
req.set_form_data({})
# or
req.body = "{}"
抱歉,我不确定。
Header 的键必须是字符串而不是符号
header = {'api-key' => 'my api key','Content-Type' => 'application/json', 'Accept' => 'application/json'}
另一个问题是 net/http
会自动将 header 大写,api-key
-> Api-Key
导致您的服务器出现 Authorization Error
。一种解决方案是创建新的 class 来包装 api-key
以防止 Ruby 那样做
class HeaderCaseSensitive < String
def capitalize
self
end
def split(*args)
super.each do |str|
HeaderCaseSensitive.new(str)
end
end
def to_s
self
end
end
然后更改header:
header = {HeaderCaseSensitive.new('api-key') => 'xxxx','Content-Type' => 'application/json', 'Accept' => 'application/json'}
总而言之,以下代码有效:
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
class HeaderCaseSensitive < String
def capitalize
self
end
def split(*args)
super.each do |str|
HeaderCaseSensitive.new(str)
end
end
def to_s
self
end
end
@toSend = {}.to_json
uri = URI.parse("https://api.riskscreen.com/api/v1/user/tokens")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {HeaderCaseSensitive.new('api-key') => 'xxx','Content-Type' => 'application/json', 'Accept' => 'application/json'}
https.set_debug_output($stdout)
req = Net::HTTP::Post.new(uri.path, header)
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "------------"
puts "Response #{res.code} #{res.message}: #{res.body}"
我将访问 Riskscreen api 以验证用户身份。为了测试 api,我编写了一个 ruby 代码片段来进行示例 POST 调用,以获取我从 Riskscreen api.
获得的令牌数量我的代码是:
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
@toSend = {}.to_json
uri = URI.parse("https://api.riskscreen.com/api/v1/user/tokens")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {'api-key': 'my api key','Content-Type': 'application/json', 'Accept': 'application/json'}
req = Net::HTTP::Post.new(uri.path, header)
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "------------"
puts "Response #{res.code} #{res.message}: #{res.body}"
但我收到以下错误:
Response 400 Bad Request
如果我将 header 行更改为
header = {'api-key'=> 'my-api-key','Content-Type'=> 'application/json', 'Accept'=> 'application/json'}
然后我收到这个错误:
Response 401 Unauthorized
坚持一段时间。请帮我解决这个问题。
你能试试删除吗:
req.body = "[ #{@toSend} ]"
并替换为:
req.set_form_data({})
# or
req.body = "{}"
抱歉,我不确定。
Header 的键必须是字符串而不是符号
header = {'api-key' => 'my api key','Content-Type' => 'application/json', 'Accept' => 'application/json'}
另一个问题是 net/http
会自动将 header 大写,api-key
-> Api-Key
导致您的服务器出现 Authorization Error
。一种解决方案是创建新的 class 来包装 api-key
以防止 Ruby 那样做
class HeaderCaseSensitive < String
def capitalize
self
end
def split(*args)
super.each do |str|
HeaderCaseSensitive.new(str)
end
end
def to_s
self
end
end
然后更改header:
header = {HeaderCaseSensitive.new('api-key') => 'xxxx','Content-Type' => 'application/json', 'Accept' => 'application/json'}
总而言之,以下代码有效:
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
class HeaderCaseSensitive < String
def capitalize
self
end
def split(*args)
super.each do |str|
HeaderCaseSensitive.new(str)
end
end
def to_s
self
end
end
@toSend = {}.to_json
uri = URI.parse("https://api.riskscreen.com/api/v1/user/tokens")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {HeaderCaseSensitive.new('api-key') => 'xxx','Content-Type' => 'application/json', 'Accept' => 'application/json'}
https.set_debug_output($stdout)
req = Net::HTTP::Post.new(uri.path, header)
req.body = "[ #{@toSend} ]"
res = https.request(req)
puts "------------"
puts "Response #{res.code} #{res.message}: #{res.body}"