如何使用ruby发布(http post)pdf内容?

如何使用ruby发布(http post)pdf内容?,ruby,pdf,file-io,http-post,curb,Ruby,Pdf,File Io,Http Post,Curb,我试图使用下面的代码块在ruby中发布PDF的原始内容 require 'pdf/reader' require 'curb' reader = PDF::Reader.new('folder/file.pdf') raw_string = '' reader.pages.each do |page| raw_string = raw_string + page.raw_content.to_s end c = Curl::Easy.new('http://0.0.0.0:4567/pdf

我试图使用下面的代码块在ruby中发布PDF的原始内容

require 'pdf/reader'
require 'curb'

reader = PDF::Reader.new('folder/file.pdf')
raw_string = ''
reader.pages.each do |page|
  raw_string = raw_string + page.raw_content.to_s
end
c = Curl::Easy.new('http://0.0.0.0:4567/pdf_upload')
c.http_post(Curl::PostField.content('param1', 'value1'),Curl::PostField.content('param2', 'value2'), c.http_post(Curl::PostField.content('body', raw_string)))
在API实现中,尽管puts raw_字符串确认变量具有所有值,但参数[:body]似乎始终为空


另外,是否有更好的方法发布pdf内容?

关于如何构建原始字符串

而不是:

reader.pages.each do |page|
  raw_string = raw_string + page.raw_content.to_s
end
您应该能够执行以下操作之一:

raw_string = reader.pages.map(&:raw_content).join
raw_string = reader.pages.map{ |p| p.raw_content.to_s }.join
为了清晰易读,我还建议您将最后一行写在几行中:

c.http_post(
  Curl::PostField.content('param1', 'value1'),
  Curl::PostField.content('param2', 'value2'), 
  c.http_post(Curl::PostField.content('body', raw_string))
)

如果您怀疑问题在于服务器实际接收数据,因为发布端似乎是正确的,那么我们需要看到连接端的一个最小示例。实际上,如果他发送原始文件,甚至不需要使用PDF::Reader。。。他应该只发送文件的二进制内容。。。