[英]Ruby on Rails Devise - Redirecting to POST request after authentication
[英]Ruby on Rails curl POST request with user authentication
我正在嘗試向rails中的REST API發出curl post請求。 我要執行的cURL請求是:
$ curl https://api.intercom.io/events \
-X POST \
-u pi3243fa:da39a3ee5e6b4b0d3255bfef95601890afd80709 \
-H "Content-Type: application/json"
-d' { "event_name" : "invited-friend",
"created_at": 1391691571, "user_id" : "314159" }'
我已經看到了在Rails中使用net / http進行API POST請求的許多示例,但是我從未找到將-u添加到請求中的示例。 我該怎么做呢?
我們可以使用basic_auth
方法執行基本身份驗證
uri = URI('http://example.com/index.html?key=value')
req = Net::HTTP::Get.new(uri)
req.basic_auth 'user', 'pass'
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
puts res.body
弄清楚了。 對於有類似問題的人:
data = {"event_name" => "invited-friend",
"created_at" => 1391691571, "user_id" => "314159" }
uri = URI("https://api.intercom.io/events")
header = {"Content-Type" => "application/json"}
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(uri.path, header)
req.basic_auth 'pi3243fa:da39a3ee5e6b4b0d3255bfef95601890afd80709', ''
req.body = data.to_json
res = https.request(req)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.