2014-04-01 50 views
0

我想在Ruby中使用Rest-Client gem進行POST,並且我一直從Web服務獲取返回值,作爲錯誤414 Unsupported Media Type,因爲我將有效負載參數設置爲「沒有有效負載的Rest-client POST

RestClient.post 'https://username:[email protected]/api/v1/applications/44204093/publishUpdates','' ,:accept => 'json' 

的問題是,網絡服務不想要或需要的有效載荷爲這個資源,但RESTClient實現覺得需要有一個規定。

任何人都知道如何與休息客戶端做一個職位,而不必指定一個有效載荷參數?

感謝

+0

你是否能夠找到一種方法來做到這一點? – ZX12R

回答

0

對於文章中,我相信該方法的url,有效載荷,頭部

url = 'https://username:[email protected]/api/v1/applications/44204093/publishUpdates' 
header = {'Accept' => 'application/json'} 
RestClient.post url, {}, header 

這是從我的頭頂,但我絆倒了爲好。

+0

對不起,但仍然返回一個415 Unsupported媒體類型 如果我使用CURL運行相同的調用它工作正常。這是一個CURL的例子; [code] curl --request POST https:// username:password @ remotesystem/api/v1/applications/44728380/publishUpdates -v [/ code] – user3486868

0

寶石是休息客戶端1.8.0,目前最穩定的一個。 我也遇到這個錯誤 RestClient :: UnsupportedMediaType:415 Unsupported Media Type from /Users/Ben/.rvm/gems/[email protected]/gems/rest-client-1.8.0/lib/restclient /abstract_response.rb:74:in`return!'

喚起的帖子及回覆是JSON

RestClient.post(url, 
        { 
         :arg1 => var1 
        }, 
        {:Authorization => "Bearer blahblah", 
        :content_type => :json, 
        :accept => :json 
        } 
    ) 
-1

這開始在紅寶石2.4發生我。對於相同版本的rest-client,Ruby 1.9沒有這個問題。如果在調用中沒有指定有效載荷,則將內容類型標頭設置爲'application/x-www-form-urlencoded',這會導致服務器說'沒有操作匹配請求路徑...'並返回'415 Unsupported媒體類型'。解決方案是明確地設置內容類型爲零在通話中:

RestClient::Request.execute(:url => query, :ssl_version => 'TLSv1', :method => 'post', :headers => {'Content-Type' => nil})