标签: net-http

用法拉第作为参数发布图像

我有旧服务和新服务,上面有图片.我想将图像从旧迁移到新的.为此我需要从我的旧服务中获取图像,然后将其发布到新的sinatra服务.我能够以我想要使用NET :: HTTP的格式发布图像

  url = URI.parse('http://0.0.0.0:9292/')
  File.open("./Belgium.png") do |jpg|
   req = Net::HTTP::Post::Multipart.new url.path, {"image" => UploadIO.new(jpg, "image/jpeg", "Belgium.jpg"), id:4234}
    res = Net::HTTP.start(url.host, url.port) do |http|
      http.request(req)
    end
  end
Run Code Online (Sandbox Code Playgroud)

我在Sinatra服务上用post post方法得到的参数是

params = {"image"=>
  {:filename=>"Belgium.jpg", :type=>"image/jpeg",:name=>"image",:tempfile=> #<File:/var/folders/h7/f9cvygqs3lg78kwh24v8yzp80000gn/T/RackMultipart20120827-76057-1f3inkn>,
 :head=> "Content-Disposition: form-data; name=\"image\";filename=\"Belgium.jpg\"\r\nContent-Length: 28228\r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n"},
  "id"=>"4234"}
Run Code Online (Sandbox Code Playgroud)

但我无法用法拉第获得相同的结果.我尝试过使用类似的东西:

payload = { :profile_pic => Faraday::UploadIO.new('avatar.jpg', 'image/jpeg') }
Faraday.post 'http://0.0.0.0:9292/', payload
Run Code Online (Sandbox Code Playgroud)

在我的服务中我得到这样的东西:
"#<'UploadIO:0x007fb950373de8>"一个对象.这不是我期望的服务.

我怎样才能获得与Net :: HTTP相同的行为.我认为它已经用中间件做了一些事情,我试过调整,但我仍然是一个新手.

ruby-on-rails sinatra net-http faraday

4
推荐指数
1
解决办法
2566
查看次数

如何在GO中使用CA证书创建TLS客户端?

我想 在GO中使用net / http创建一个tls客户端,如何给定ca证书来创建它呢?

ssl https go net-http

4
推荐指数
1
解决办法
2501
查看次数

使用 ruby​​ Net:HTTP API 将文件上传到远程 apache 服务器因 409 冲突而失败

下面是一个将文件从本地文件系统上传到远程 Apache 服务器的程序。

程序以 409 冲突错误结束。有什么建议我做错了什么吗?我在 httpd.conf 中打开了 DAV 并授予了所有必要的权限,但我仍然没有运气。如果需要,我可以发布 httpd.conf。

这是代码:

BOUNDARY = "AaB03xZZZZZZ11322321111XSDW"
uri = URI.parse("http://localhost/dropbox/")
file = "/tmp/KEYS.txt"
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new(uri.request_uri)
request.body_stream=File.open(file)
request["Content-Type"] = "multipart/form-data"
request.add_field('Content-Length', File.size(file))
request.add_field('session', BOUNDARY)
response=http.request(request)
puts "Request Headers: #{request.to_hash.inspect}"
puts "Sending PUT #{uri.request_uri} to #{uri.host}:#{uri.port}"
puts "Response #{response.code} #{response.message}"
puts "#{response.body}"
puts "Headers: #{response.to_hash.inspect}"
Run Code Online (Sandbox Code Playgroud)

它的输出:

Request Headers: {"accept"=>["*/*"], "host"=>["localhost"], "content-length"=>[88873],     "content-type"=>["multipart/form-data"], "session"=>["AaB03xZZZZZZ11322321111XSDW"],   "connection"=>["close"]}
Sending PUT /dropbox/ to localhost:80
Response 409 Conflict
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head> …
Run Code Online (Sandbox Code Playgroud)

ruby http-put net-http

3
推荐指数
1
解决办法
4922
查看次数

在Rails中使用NetHTTP来命中Rails

我们有一个非常独特的用例,我们希望Rails控制器使用Net :: HTTP访问Rails应用程序中的路由.可以这样做吗?我正在尝试这样做时收到超时.当uri是一个单独的Rails应用程序时,当前代码有效,但当uri属于应用程序本身时则不行.这是当前控制器操作的要点:

def export_data
  uri = URI("http://localhost:3000")
  @data = JSON.parse( Net::HTTP.get(uri) )

  respond_to do |format|
    ...
  end
end
Run Code Online (Sandbox Code Playgroud)

忘记我们为什么要这样做.为什么这不起作用?是否可以进行修改以使其工作?提前致谢!

ruby ruby-on-rails net-http

3
推荐指数
1
解决办法
453
查看次数

机械化获取"Errno :: ECONNRESET:通过对等方重置连接 - SSL_connect"

我无法让Mechanize加载以前工作的页面 - 它可靠地失败并显示Errno: ECONNRESET: Connection reset by peer - SSL_connect消息.关于我应该尝试什么或者我应该看的细节的任何建议?(请参阅下面的"我尝试过的内容")

更新1

相关的SO帖子中提示,我尝试直接访问该网站Net::HTTP.当我设置时http.ssl_version = :TLSv1,我得到一个重定向而不是一个错误(应该是).所以我的问题变成:如何ssl_version从内部配置Net :: HTTP的底层参数Mechanize

谢谢...

症状:

$ rails console
>> a = Mechanize.new
=> #<Mechanize:0x007fd26789b8e0 ...>
>> p = a.get("http://sce.com")
# (...after a long pause...)
Errno::ECONNRESET: Connection reset by peer - SSL_connect
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
from /sandbox/usr/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:857:in `start'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:691:in `start'
from …
Run Code Online (Sandbox Code Playgroud)

ssl openssl net-http mechanize-ruby ruby-on-rails-4

3
推荐指数
1
解决办法
5698
查看次数

使用Net/http发布JSON格式的Ruby数据

我有这个ruby文件:

require 'net/http'
require 'json'
require 'uri'
    #test data

newAcctJson ='{
  "type": "Credit Card",
  "nickname": "MoreTesting",
  "rewards": 2,
  "balance": 50
}'

    #creates a new account
def createAcct(custID, json)
    url = "http://api.reimaginebanking.com:80/customers/#{custID}/accounts?key=#{APIkey}"
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    myHash = JSON.parse(json)
    resp = Net::HTTP.post_form(uri, myHash)
    puts(resp.body)
end
Run Code Online (Sandbox Code Playgroud)

试图创建一个新帐户.但是我得到了code: 400, invalid fields in account.我独立测试了数据,所以我(相对)确定json本身的格式不正确; 问题是尝试以所需的哈希格式提交数据post_Form.有没有人知道如何使用ruby直接发布json数据而不先转换为哈希?

ruby json net-http

3
推荐指数
1
解决办法
1万
查看次数

如何使用Net :: Http下载包含UTF-8字符的文件?

我有一个应用程序,用户可以上传保存到S3的基于文本的文件(xml,csv,txt).其中一些文件非常大.需要对这些文件中的数据执行各种操作,因此不是从S3中读取它们,而是偶尔将其超时我在本地下载文件,然后将操作放在上面.

这是我用来从S3下载文件的代码.Upload是我用来存储此信息的AR模型的名称.此方法是Upload模型上的实例方法:

def download
  basename = File.basename(self.text_file_name.path)
  filename = Rails.root.join(basename)
  host = MyFitment::Utility.get_host_without_www(self.text_file_name.url)
  Net::HTTP.start(host) do |http|
    f = open(filename)
    begin
      http.request_get(self.text_file_name.url) do |resp|
        resp.read_body do |segment|
          f.write(segment) # Fails when non-ASCII 8-bit characters are included.
        end
      end
    ensure
      f.close()
    end
  end
  filename

end
Run Code Online (Sandbox Code Playgroud)

所以你看到负载失败的上面一行.此代码以某种方式认为所有下载的文件都以ASCII 8位编码.我怎么能够:

1)检查远程文件的编码,例如2)下载并成功写入.

这是现在特定文件发生的错误:

Encoding::UndefinedConversionError: "\x95" from ASCII-8BIT to UTF-8
from /Users/me/code/myapp/app/models/upload.rb:47:in `write'
Run Code Online (Sandbox Code Playgroud)

感谢您提供的任何帮助!

ruby encoding amazon-s3 character-encoding net-http

3
推荐指数
1
解决办法
789
查看次数

Ruby Net::HTTP 执行已过期

使用 Net::HTTP,我定期发现下面的代码从 StandardError 中拯救了一条“执行已过期”的消息,尽管来自访问的 URL 的 Web 服务器日志显示相应的响应被快速发送。当 Web 服务器日志显示响应时间超过 5 秒时,我通常会看到 Timeout::Error 中的代码救援。

什么情况会导致下面的代码从 StandardError 中拯救出“执行已过期”而不是从 Timeout::Error拯救出来?

此代码在一个相对古老的 Ruby 1.9.3 上的多线程程序中运行,该平台不支持较新版本的 Ruby。尽管该程序是多线程的,但显示的代码仅在单线程上运行。

begin
  connection = Net::HTTP.new(uri.host, uri.port)
  connection.open_timeout = 5
  connection.read_timeout = 5
  connection.start do |http|
    request = Net::HTTP::Post.new("/reader_events")
    request.body = body
    response = http.request(request)
  end
rescue StandardError => std_error
  log "error sending event to server: #{std_error}"
rescue Timeout::Error => error
  log "timeout sending event to server"
end
Run Code Online (Sandbox Code Playgroud)

ruby net-http

3
推荐指数
1
解决办法
1万
查看次数

Ruby Net::HTTP::Post 提交表单

我正在创建一个网站抓取工具。有一个用于更改当前页面的表单。

这是我为 POST 请求提交表单的方式,但它似乎一遍又一遍地获取相同的页面。

下面是一些示例代码:

pages = {
 "total_pages" => 19,
 "p1" => '1234/1456/78990/123324345/12143343214345/231432143/12432412/435435/',
 "p2" => '1432424/123421421/345/435/6/65/5/34/3/2/21/1243',
..
..
..    
}


idx = 1
p_count = pages["total_pages"]

#set up the HTTP request to change pages to get all the auction results
uri = URI.parse("http://somerandomwebsite.com?listings")
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.request_uri)

p_count.times do
  puts "On loop sequence: #{idx}"
  pg_num = "p#{idx}"
  pg_content = pages["#{pg_num}"]
  req.set_form_data({"page" => "#{pg_num}", "#{pg_num}" => "#{pg_content}"})

  response = http.request(req)
  page = Nokogiri::HTML(response.body)
  idx = idx + …
Run Code Online (Sandbox Code Playgroud)

ruby forms post net-http

3
推荐指数
1
解决办法
3682
查看次数

http在golang中覆盖http标头代码,而json编码有错误

考虑这种情况!

成功执行http请求后,如果执行json编码出错怎么办,如何覆盖头代码

func writeResp(w http.ResponseWriter, code int, data interface{}) {
    w.Header().Set("Content-Type", "application/json")

    //Here I set the status to 201 StatusCreated
    w.WriteHeader(code) 
    s := success{Data: data}

    //what if there is an error here and want to override the status to 5xx error
    //how to handle error here, panic?, http.Error() is not an option because as we already wrote header to 201, it just prints `http: multiple response.WriteHeader calls`
    if err := json.NewEncoder(w).Encode(s); err != nil {
        w.Header().Set("Content-Type", "application/json")

        //it throws http: …
Run Code Online (Sandbox Code Playgroud)

error-handling http go net-http

3
推荐指数
1
解决办法
3582
查看次数