Ruby on rails 将文件从ActiveStorage上载到API

Ruby on rails 将文件从ActiveStorage上载到API,ruby-on-rails,rails-activestorage,httparty,Ruby On Rails,Rails Activestorage,Httparty,我正在尝试将一个文件上载到带有HttpParty gem的API。 以下是此API文档中要求的格式: Method: POST Content-Type: application/json { "name": "filename.png", "type": 2, "buffer": "iVBOR..." } 我的文档存储在ActiveStorage中,下面是我下载文档并生成参数

我正在尝试将一个文件上载到带有HttpParty gem的API。 以下是此API文档中要求的格式:

Method: POST
Content-Type: application/json   
{
  "name": "filename.png",
  "type": 2,
  "buffer": "iVBOR..."
}
我的文档存储在ActiveStorage中,下面是我下载文档并生成参数哈希的函数:

def document_params
  {
    "name": @document.file.filename.to_s,
    "type": IDENTIFIERS[@document.document_type],
    "buffer": @document.file.download
  }
end
然后我使用此功能发送数据:

HTTParty.post(
  url,
  headers: request_headers,
  body: document_params.to_json
)
问题是,当我对json执行
文档参数时,会出现以下错误:

未定义的转换器错误(从ASCII-8BIT到UTF-8的“\xC4”)

如果我不调用_json,数据将不会作为有效的json发送,而是作为如下的哈希表示:
{:key=>“value”}


我只想将文件数据作为二进制数据发送,而不尝试将其转换为UTF-8。

我找到了一个解决方案:将文件内容编码为Base64:

def document_params
  {
    "name": @document.file.filename.to_s,
    "type": IDENTIFIERS[@document.document_type],
    "buffer": Base64.encode64(@document.file.download)
  }
end

我找到了一个解决方案:将文件内容编码为Base64:

def document_params
  {
    "name": @document.file.filename.to_s,
    "type": IDENTIFIERS[@document.document_type],
    "buffer": Base64.encode64(@document.file.download)
  }
end