- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在远程主机上,我有一个 bash 脚本将一个简单的 gzip 压缩 YAML 文件发送到我的 Ruby Sinatra 端点:
#!/bin/bash
/bin/gzip -c /tmp/test.yaml > /tmp/test.gz
curl -i <hostname>:<port>/last_run_report -H "Content-Type: application/xml" -H "Content-Encoding: gzip" --data-binary @/tmp/test.gz
我的示例 Ruby 应用程序是:
require 'sinatra'
require 'zlib'
require 'stringio'
set :port, <port>
set :bind, "<ip>"
post '/last_run_report' do
sio = StringIO.new(request.body.to_s)
gz = Zlib::GzipReader.new(sio).read
test_yaml = YAML.load(gz)
end
这给了我以下错误:
Zlib::GzipFile::Error: not in gzip format
如果我需要“base64”并将端点定义更改为:
post '/last_run_report' do
raw = Base64.decode64(request.body)
sio = StringIO.new(raw)
gz = Zlib::GzipReader.new(sio).read
test_yaml = YAML.load(gz)
end
我收到以下错误:
NoMethodError: undefined method `unpack1' for #<StringIO:0x000055713e2d51b8>
我不知道是我发送的数据有误还是读取有误。请帮忙。
最佳答案
假设 YAML 样本如下所示:
martin:
name: Martin D'vloper
job: Developer
skill: Elite
您不需要所有多余的 StringIO
东西。 request.body
已经是一个 StringIO
实例,因此不需要将其转换为字符串,然后再将其转换为 StringIO
:
require 'sinatra'
require 'zlib'
post '/last_run_report' do
gz = Zlib::GzipReader.new(request.body).read
puts YAML.load(gz)
end
现在提出您的要求:
curl -i localhost:4567/last_run_report -H "Content-Type: application/xml" -H "Content-Encoding: gzip" --data-binary @test.gz
并查看 sinatra 输出:
== Sinatra (v2.0.4) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
{"martin"=>{"name"=>"Martin D'vloper", "job"=>"Developer", "skill"=>"Elite"}}
::1 - - [14/Jan/2019:23:24:28 -0700] "POST /last_run_report HTTP/1.1" 200 - 0.0048
注意 puts
已经将 {"martin"=>{"name"=>"Martin D'vloper", "job"=>"Developer", "skill"=>"Elite"}}
写入了控制台。
我应该指出,在您的示例中,以下代码无法按您期望的方式工作:
sio = StringIO.new(request.body.to_s)
您希望能够调用 sio.read
并获得如下内容:
\x1F\x8B\b\b\xA7z=\\\x00\x03test.yaml\x00SVp\xCCSH\xCD-\xC8\xC9\xAFLMU(JM\xCE/J\xE1\xCAM,*\xC9\xCC\xB3\xE2R\x00\x82\xBC\xC4\xDCT+\x05_\xB0\x88\x82\x8BzYN~Aj\x11X&+?\xC9J\xC1%\xB5,\x15!T\x9C\x9D\x99\x93c\xA5\xE0\x9A\x93Y\x92\n\x00\xFC\xA0\x83yZ\x00\x00\x00
你实际上得到的是:
#<StringIO:0x00007ffd8184bdf0>
请注意,这是文字字符串“#<StringIO:0x00007ffd8184bdf0>
”而不是对 StringIO
对象的引用,因为这是在像 .to_s
这样的 StringIO
对象上调用 request.body
时返回的内容,因此对 sio.read
的任何后续调用(隐含在调用 Zlib::GzipReader.new
) 将返回该文字字符串,并且不会返回您期望它返回的 gzip 数据,这会导致错误 Zlib::GzipFile::Error: not in gzip format
。
当你想返回 StringIO
的字符串表示时,你应该调用 .string
或 .read
,而不是 .to_s
。考虑到这一点,使您的第一个示例工作所需的最小更改是更改此内容:
sio = StringIO.new(request.body.to_s)
对此:
sio = StringIO.new(request.body.string)
但同样,这是 StringIO
到字符串再返回到 StringIO
的不必要转换。
关于ruby - 如何在 Ruby Sinatra 中读取 GZIP 负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54192368/
您在 Sinatra 应用程序中使用什么验证码? 我喜欢 Google 的 http://www.google.com/recaptcha ,但似乎不适用于 Sinatra(不过有一个 Rails 插
如何从命令行将 Sinatra 应用作为守护程序运行? 它正在使用瘦: ruby app.rb -p 3000 -e production 我不喜欢在 app.rb 本身中设置它。我想从命令行取消它。
我正在使用 Sinatra 在 Ruby 中编写一个小型 Web 服务。使用 http 基本身份验证(在生产中通过 https)控制对几乎所有内容的访问。 我想从要求授权中排除一个特定的目录。是否有捷
我曾经能够在启动 sinatra 应用程序的窗口中看到为 sinatra 发出的 http 请求。 我认为在 sinatra 升级后我再也看不到它们了,我不知道如何做到这一点。我不需要将它们记录到文件
带有 activerecord gem 的 Sinatra 项目发出警告: DEPRECATION WARNING: Currently, Active Record suppresses error
我正在玩 Sinatra,我想让我的一条路线不区分大小写。我尝试添加这样的路线: get "(?i)/tileflood/?" do end 但它与预期的/tileflood 的任何排列都不匹配。我在
如果我没有在 Sinatra 中明确设置 session 过期时间,那么默认的过期时间是多少? 最佳答案 Sinatra session 来自 Rack::Session;所以开箱即用它默认为不过期。
似乎没有关于如何使用 Sinatra 中的 Rack::Protection 为表单启用 CSRF 保护的文档。有人做过这个吗?我似乎不知道如何告诉 Sinatra 启用此功能。 最佳答案 Sinat
假设我有一条 Sinatra 路线: put '/data' do request.body.read # ... end 看起来整个 request.body 都被读入内存了。有没有办法在主
我正在 Sinatra 中运行一个经典风格的应用程序,我想获取该应用程序绑定(bind)到的 URL。例如,如果我在开发环境中启动它,我希望得到:http://localhost:4567/,而在生产
我正在尝试在我的 Mac 上安装以下 ruby gem: http://www.sinatrarb.com/intro.html 我收到以下消息: michael-rosarios-mac
我最近更新到了 Ruby 1.9.2 (RVM)、Sinatra 1.1 和 Passenger 3.0.0。我有一个简单的应用程序,由以下部分组成: # config.ru require 'rub
似乎没有关于如何使用 Sinatra 中的 Rack::Protection 为表单启用 CSRF 保护的文档。有人做过这个吗?我似乎不知道如何告诉 Sinatra 启用此功能。 最佳答案 Sinat
与 sinatra 应用程序相比,Rack 应用程序有哪些优势? 谢谢 最佳答案 我认为提问者想要将它保留在 Rack 上的优点。 考虑到 Sinatra 已经是典型 Web 应用程序的极薄和简约的贴
我最近创建了一个带有登录表单(无基本身份验证)的 Sinatra 应用程序。为了防止在用户登录之前访问该应用程序,我在适当的位置放置了一个 before block before do unles
我如何设置 Sinatra 以便公用文件夹中的静态文件与响应 Access-Control-Allow-Origin = "*"一起返回? 最佳答案 在这里查看这个问题:Sinatra OPTIONS
我有一个基于 Sinatra 的 REST 服务应用程序,我想从其中一个路由中调用其中一个资源,从而有效地将一个资源与另一个资源组合在一起。例如 get '/someresource' do ot
我有一个使用经典 Sinatra 的旧项目(不是模块化 我在互联网上能找到的唯一使用 Puma 的示例是使用模块化 Sinatra。 引用:http://www.sinatrarb.com/intro
问题是默认处理程序是在 中定义的。 Sinatra::ShowExceptions ,由 控制:raise_errors 配置设置,这个设置是全有或全无。如果 :raise_errors 关闭然后 错
我有以下 Sinatra 应用程序,我正在测试 error阻止但它似乎不起作用。 这是我的 sinatra 应用程序: require 'rubygems' require 'sinatra' err
我是一名优秀的程序员,十分优秀!