2014-10-07 17 views
-1

我張貼到接受捲曲如下的API:如何使用RestClient作爲URL參數發佈?

curl -v -X POST "https://url.com" -d 'input=frustrated&user_key=3b9ccb48e734fce6b982a9c1c2cef301' 

我試圖用一個錯誤如下:

data = {'user_key' => "#{ENV['USER_KEY']}", 'input' => "#{text}", 'client_name'=>> "#{client_name}"} 
    talkresponse = JSON.parse(RestClient.post url_talk_bot, {:params => data}) 

出於某種原因,該數據是罰款所有除了「輸入',它總是以數組的形式得到一個錯誤,由於字符串是預期的,所以它會觸發一個錯誤。請注意下面的輸入參數是一個數組。

{"user_key"=>"3b9ccb48e734fce6b982a9c1c2cef301", "input"=>"[\"frustrated how do I post to params, worked fine before\"]", "client_name"=>"14155086888"} 

/mnt/task/__gems__/gems/rest_client-1.7.3/lib/restclient/abstract_response.rb:48:in `return!': 401 Unauthorized (RestClient::Unauthorized) 
+0

什麼是錯誤? – 2014-10-07 19:57:32

+1

爲什麼不使用[Curb](https://github.com/taf2/curb),它很好地包裝了libcurl。 – 2014-10-07 20:45:28

+0

+1 to'curb'。更好的是,如果你做了很多HTTP工作:[法拉第](https://github.com/lostisland/faraday)。 – tadman 2014-10-07 20:47:29

回答

0

我在使用RestClient.post時遇到了類似的問題。 正因如此,我停止了使用。員額,並開始使用.execute

  • RestClient.post URI,{:PARAMS =>數據}

成爲

  • RestClient.execute ({:method =>:post,:url => uri,:headers => {api_key:'123',authorization:Base64 :: encode(「#{user}:#{password}」},:payload:body })
+0

呃---是否意味着要查找其他客戶端以外的其他http客戶端? – Angela 2014-10-08 19:09:58

相關問題