如何转换卷曲请求红宝石宝石了HTTPClient

问题描述 投票:2回答:1

我想转换此CURL要求:

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错误的请求我所有的努力的结果。以下是我已经试过:

  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}

有任何想法吗?

ruby-on-rails ruby curl rest-client
1个回答
0
投票

您的要求是不等价的。我猜你是混合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'})

看起来你的卷曲示例使用application/x-www-form-urlencoded,而你的Ruby示例使用multipart/form-data

对于一些背景资料,检查

© www.soinside.com 2019 - 2024. All rights reserved.