简体   繁体   English

http发布请求和Rails中的授权标头声明

[英]http Post request And declaration of authorization header in rails

I am trying to perform an HTTP authorization using Ruby on Rails. 我正在尝试使用Ruby on Rails执行HTTP授权。 Here is what I'm trying: 这是我正在尝试的:

res = http.post(uri.request_uri, 
:Authorization => cobSessionToken,
"coBrandSessionCredential=loginToken=#{cobSessionToken}&userLogin=#{login}&userPassword=#{password}")
render :json =>  {"isValid" => true, "Body" => JSON.parse(res.body)}

This doesn't seem to work. 这似乎不起作用。 How can I perform an authorization? 如何执行授权?

how about something like this? 这样的事情怎么样?

url = URI.parse('https://my.url.com/path')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'user', 'pass'
req.use_ssl = true
req.form_data({'key1' => 'val1', 'key2' => 'val2'})

resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
puts resp

I would recommend using something like postman (its a free google program you can get at the google store) to make sure the error is not on the server side. 我建议您使用类似邮递员的东西(它是一个免费的Google程序,您可以从Google商店获得)以确保错误不在服务器端。 Use Net:http it comes with ruby so you do not need to install it but you have to require it. 使用Net:http随附的ruby,因此您无需安装它,但必须使用它。 Require it by: require "net/http" require "uri" Use this cheatsheet I think you need basic_auth.rb You will see how to form the request. 要求者: require "net/http" require "uri"使用此备忘单,我认为您需要basic_auth.rb您将看到如何形成请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM