【问题标题】:How to convert a cURL multipart/form-data POST request to ruby request using rest-client?如何使用 rest-client 将 cURL multipart/form-data POST 请求转换为 ruby​​ 请求?
【发布时间】:2019-05-28 04:16:14
【问题描述】:

我必须使用 Rest-Client 在 Ruby 中实现下面列出的 curl POST 请求,以便我可以将文件上传到我的服务器。

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'Authorization: Basic jhdsjhdshjhdshdhh' --header 'tenant-code:djdjhhsdsjhjsd=' {"type":"formData"} 'https://myserver.com/blobs/upload?fileName=some.ipa&groupId=098'

我已经尝试了以下 -

require 'rest-client'
require 'json'

payload = {
    :multipart => true,
    :file => File.new(path_to_file, 'rb')
}

response = RestClient.post("https://myserver.com/blobs/upload?fileName=some.ipa&groupId=098", payload, {accept: :json, 'tenant-code':"djdjhhsdsjhjsd=", 'Authorization': "Basic jhdsjhdshjhdshdhh"})

这可以正常工作,因为我从服务器收到 200 响应。但是,当我在我的服务器上检查上传的文件时,它已损坏。

这是使用 ruby​​ 进行多部分/表单数据请求的正确方法吗?

我看到 cURL 请求中有以下内容,这在任何地方的 ruby​​ 请求中都没有考虑。

{"type":"formData"}

我需要为此做些额外的事情吗?

【问题讨论】:

    标签: ruby curl multipartform-data multipart rest-client


    【解决方案1】:

    我通过将请求更改为以下内容解决了我的特定情况(我需要将 IPA 文件上传到我的企业应用商店):-

    require 'rest-client'
    require 'json'
    
    response = RestClient::Request.execute(
      :url => "https://myserver.com/blobs/upload?fileName=some.ipa&groupId=098",
      :method => :post,
      :headers => {
        'Authorization' => "Basic jhdsjhdshjhdshdhh",
        'tenant-code' => "djdjhhsdsjhjsd=",
        'Accept' => 'application/json',
        'Content-Type' => 'application/octet-stream',
        'Expect' => '100-continue'
      },
      :payload => File.open(path_to_file, "rb")
    )
    

    基本上,内容类型需要是八位字节流,Expect header也需要设置。

    希望它可以帮助那些来寻找这样的东西的人。

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 1970-01-01
      • 2017-10-31
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      相关资源
      最近更新 更多