1
返回狀態這外殼爲我的作品:如何發佈一個文件,並HTTParty
export TOKEN='eyJqdGkiOiIzZDkzNz'
curl https://apis.xxxxxxxx.com/v2-beta/media \
--header "Authorization: Bearer $TOKEN" \
--form 'configuration={"configuration" : {"executor": "v2"}}' \
--form media="@recording.mp3" \
| tee media-post-response.json | jq '.'
export MEDIA_ID=$(jq --raw-output '.mediaId' < media-post-response.json)
echo "Uploaded file with mediaId = ${MEDIA_ID}"
我想要做HTTParty同樣的事情,但是,我可以在任何地方找不到的例子,顯示了這些頭文件應該被指定。
這裏是我有什麼,但它拋出一個JSON錯誤:
require 'httparty'
require 'json'
require 'pp'
token = "6Ijg3ZDMzODUxLTdlMmMtN"
url = "https://apis.xxxxxxxx.com/v2-beta/media"
options = {
:headers => {"Authorization" => " Bearer #{token}", "Content-Type" => "multipart/form-data" , "Accept" => "application/json"},
:body => {"media" => '@recording.mp3'}
}
puts "***** #{options} ******\n"
j = HTTParty.post(url, options)
s = JSON.parse(j.body)
pp s
的錯誤是:
(error) .rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/json/common.rb:156:in `parse': 784: unexpected token at 'Failed to create MuleMessage (org.mule.api.lifecycle.CreateException)' (JSON::ParserError)
你的捲曲命令具有'--form「配置= { 「配置」:{ 「執行者」: 「V2」}} ''但這似乎從你的Ruby代碼中缺少。也許這需要嗎?另外,你在Ruby代碼中的'Bearer'之前有一個神祕的空間,但是我懷疑這是個問題。 –
哦,我剛剛意識到你正在嘗試上傳文件。 '@'語法('@ recording.mp3')是捲曲特定的,不會在HTTParty中工作。它看起來像HTTParty不假設分段上傳,但有一個稱爲httpmultiparty的gem來完成這項工作。看看這個線程:https://github.com/jnunemaker/httparty/issues/77 –
謝謝,我看到httmulitparty,但它似乎是某種不受支持(遺棄?)寶石。即使使用它,我也看不到如何正確設置標題。這是行不通的:options = {0} {0} {「Authorization」=>「Bearer#{token}」}, :media => File.new('recording.mp3') } – jacksonp