2014-01-20 73 views
0

我有以下代碼:紅寶石百頭巨怪的寶石 - 無效Json的身體?

require 'Typhoeus' 

    url = Typhoeus::Request.new("https://fluidsurveys.com/api/v2/webhooks/subscribe/", 
     userpwd: "username_test:password_test", 
     method: :post, 
     body: { 
      'subscription_url' => 'http://glacial-spire-test.herokuapp.com/hooks/response_created_callback', 
      'event' => 'response_complete' 
     }, 
     headers: { 'Content-Type' => "application/json"}) 

    response = url.run.body 
    puts response 

這將返回的400響應代碼和錯誤:

Content-Type set to application/json but could not decode valid JSON in request body.

下面是API調用我正在文檔:http://docs.fluidsurveys.com/api/webhooks.html

POST /api/v2/webhooks/subscribe/¶ 
Returns a status of 409 if a webhook with the subscription url already exists. Returns a  
status of 201 if the webhook was successfully created. Requests must be sent as an 
application/json-encoded dictionary with the required fields subscription_url and event 

樣品請求(根據DOCS):

{ 
    "subscription_url": "http://fluidsurveys.com/api/v2/callback/", 
    "event": "response_complete", 
    "survey": 1, 
    "collector": 1 
} 

我在這裏做錯了什麼?調查和收集器是可選的參數,可以和我沒有看到一個問題,在我的身體JSON。

回答

1

我猜你可能需要使用庫來請求體轉換成JSON像Ojhttps://github.com/ohler55/oj)。使用Oj庫:

requestBody = { 
    'subscription_url' => 'http://glacial-spire-test.herokuapp.com/hook/response_created_callback', 
    'event' => 'response_complete' 
} 

url = Typhoeus::Request.new("https://fluidsurveys.com/api/v2/webhooks/subscribe/", 
     userpwd: "username_test:password_test", 
     method: :post, 
     body: Oj.dump(requestBody, mode: :compat), 
     headers: { 'Content-Type' => "application/json"}) 

response = url.run.body 
puts response 

的臨界線爲:

 body: Oj.dump(requestBody, mode: :compat) 

如果需要加載任何JSON內容紅寶石,只需使用Oj.load

+0

它的魔力,那就是了!謝謝。 – Luigi