簡體   English   中英

Rest-Client:如何發布多部分/表單數據?

[英]Rest-Client: how to post multipart/form-data?

我必須使用Rest-Client在Ruby中實現下面列出的curl POST請求。

我必須:

  • 在標頭中發送參數;
  • 發送參數(不包含文件)作為multipart/form-data

     $ curl -X POST -i -H "Authorization: Bearer 2687787877876666686b213e92aa3ec7e1afeeb560000000001" \\ https://api.somewhere.com/endpoint -F sku_id=608399 

如何使用RestClient ruby​​gem轉換curl請求?

閱讀文檔(多部分): https : //github.com/rest-client/rest-client我編碼為:

@access_token = 2687787877876666686b213e92aa3ec7e1afeeb560000000001
url = 'https://api.somewhere.com/endpoint'
req = { authorization: "Bearer #{@access_token}"}


RestClient.post url, req, {:sku_id => 608399, :multipart => true}

但是我收到服務器錯誤; 上面的Ruby代碼正確嗎?

非常感謝,喬治

由於我無法理解Dmitry顯示的示例,因此以下是創建用於上傳圖像的Multipart請求的示例:

response = RestClient.post 'https://yourhost.com/endpoint',

{:u_id => 123, :file => File.new('User/you/D/cat.png', 'rb'), :multipart => true},

{:auth_token => xyz5twblah, :cookies => {'_cookie_session_name' => cookie}}

該代碼對RestClient實現無效。 headers應在payload之后。

module RestClient 
 def self.post(url, payload, headers={}, &block)
  ...
 end
end

更新

@access_token應該是字符串"2687787877876666686b213e92aa3ec7e1afeeb560000000001"

然后

RestClient.log = 'stdout'
RestClient.post url, {:sku_id => 608399, :multipart => true}, req

並記錄

RestClient.post "https://api.somewhere.com/endpoint", "--330686\r\nContent-Disposition: form-data; name=\"sku_id\"\r\n\r\n608399\r\n--330686--\r\n", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Authorization"=>"Bearer 2687787877876666686b213e92aa3ec7e1afeeb560000000001", "Content-Length"=>"79", "Content-Type"=>"multipart/form-data; boundary=330686"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM