如何将CURL请求转换为Ruby Gem HTTPClient

zaq34kh6  于 12个月前  发布在  Ruby
关注(0)|答案(2)|浏览(94)

我正在尝试转换此URL请求:

curl \
  -F 'slideshow_spec={ 
    "images_urls": [ 
      "<IMAGE_URL_1>", 
      "<IMAGE_URL_2>", 
      "<IMAGE_URL_3>" 
    ], 
    "duration_ms": 2000, 
    "transition_ms": 200 
  }' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://google.com

一个HTTPClient请求,但我所有的尝试都导致400 Bad Request。以下是我尝试过的:

payload = {
        "images_urls": [
          "https://cdn-m2.esoftsystems.com/10100028/TAASTRUP%40DANBOLIG.DK/10106239925/160596797/BEST_FIT/1542/1024/IMG_5511.jpg",
          "https://cdn-m2.esoftsystems.com/10100028/TAASTRUP%40DANBOLIG.DK/10106239925/160596797/BEST_FIT/1542/1024/IMG_5511.jpg",
          "https://cdn-m2.esoftsystems.com/10100028/TAASTRUP%40DANBOLIG.DK/10106239925/160596797/BEST_FIT/1542/1024/IMG_5511.jpg"
        ],
        "duration_ms": 2000,
        "transition_ms": 200
      }

  response = RestClient.post url, {slideshow_spec: payload.to_json, multipart: true, access_token: access_token}

有什么想法吗?

yiytaume

yiytaume1#

你们的要求不一样。我猜你是混合JSONMultipartx-www-form-urlencoded在这里。
RestClient支持所有三种格式(示例取自https://github.com/rest-client/rest-client/blob/master/README.md

# POST JSON
RestClient.post "http://example.com/resource", {'x' => 1}.to_json, {content_type: :json, accept: :json}

# POST Multipart
# Usually not needed if you don't want to upload a file
RestClient.post '/data', {:foo => 'bar', :multipart => true}

# POST x-www-form-urlencoded
RestClient.post('https://httpbin.org/post', {foo: 'bar', baz: 'qux'})

看起来curl示例使用的是application/x-www-form-urlencoded,而Ruby示例使用的是multipart/form-data
有关一些背景信息,请查看

odopli94

odopli942#

你可以使用curlify gem。curlify gem将HTTP请求转换为curl命令。

相关问题