什么是Ruby等同于这个curl请求? [英] What is the Ruby equivalent to this curl request?
本文介绍了什么是Ruby等同于这个curl请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要发布到api。这个例子从docs工作在curl:
I'm tring to post to an api. This example from the docs works in curl:
curl -k -w %{http_code} -H "Content-Type:text/plain" -u user:pass --data-binary @filename https://server/url/here
这是我在法拉第尝试过的:
This is what I have tried with faraday:
require 'rubygems'
require 'faraday'
require 'pp'
conn = Faraday.new(:url => 'https://server/url/here' , :ssl => {:verify => false} ) do |faraday|
faraday.response :logger
faraday.basic_auth('user', 'pass')
faraday.adapter Faraday.default_adapter
end
data = File.read('teste.txt')
res=conn.post '/' , data
pp res
它发布,我收到一个200代码,但出错了。
It posts, I receive a 200 code but something goes wrong. The response is the server's login page.
推荐答案
看起来不错,唯一的区别是可能是标题中的内容类型,这应该工作:
That looks right, the only difference is possibly the content-type in the header, this should work:
require 'rubygems'
require 'faraday'
require 'pp'
conn = Faraday.new(:url => 'https://server/url/here' , :ssl => {:verify => false} ) do |faraday|
faraday.response :logger
faraday.basic_auth('user', 'pass')
faraday.adapter Faraday.default_adapter
end
data = File.read('teste.txt')
res = conn.post do |req|
req.headers['Content-Type'] = 'text/plain'
req.body = data
end
pp res
这篇关于什么是Ruby等同于这个curl请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文