2017-01-23 95 views
0

我一直在負責重建this javascript file到Ruby,但我得到的是沒有意義的(對我)一個響應的錯誤是:API沒有響應預期

{"api_error_code"=>1, "message"=>"Parameter 'username_or_email' is required.", "code"=>"API_ERROR", "target"=>"<webapp.resources.user_session_resource.UserSessionResource object at 0xb8e2a90>", "http_status"=>400} 

它不道理監守我確實供應username_or_email參數,你可以在這裏看到,我提出請求之前做身體上的看跌期權:

{"source_url":"/login/","data":"{\"options\":{\"username_or_email\":\"[email protected]\",\"password\":\"testpassword\"},\"context\":{}}","module_path":"App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large)"} 

它也似乎並不無論我怎樣設置有效載荷,無論是通過做@curl.post_body = payload@curl.http_post(payload)@curl.http_post(url,payload)它只是迴應相同的錯誤。順便提一下@curlCurl::Easy的一個實例。

我已經匹配了json數據請求,因爲原來有這麼做是怎麼回事?

這裏的請求至今:

require_relative "curb_dsl" 
require 'json' 
module Pin 
    class Client 
    Login_URL = "https://www.pinterest.com/resource/UserSessionResource/create/" 
    Repin_URL = "https://www.pinterest.com/resource/RepinResource/create/" 
    include Curb_DSL 
    class << self 
     def login(username_or_email, password) 
     self.new do 
      set_uri Login_URL 
      header 'Accept', 'application/json, text/javascript, */*; q=0.01' 
      header 'Accept-Language', 'en-US,en;q=0.5' 
      header 'Cache-Control', 'no-cache' 
      header 'DNT','1' 
      header 'Host', 'www.pinterest.com' 
      header 'Origin', 'https://www.pinterest.com' 
      header 'Referer', 'https://www.pinterest.com/' 
      header 'User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' 
      header 'X-APP-VERSION', '18733c1' 
      header 'X-CSRFToken', 'K4C0QUu35Eoq1xjajbMluw7hOKibpQSW' 
      header 'X-NEW-APP', '1' 
      header 'X-Pinterest-AppState', 'active' 
      header 'X-Requested-With', 'XMLHttpRequest' 
      set_cookies({ ":_auth" => '0',csrftoken: 'K4C0QUu35Eoq1xjajbMluw7hOKibpQSW'}) 

      set_payload({ 
      source_url: "/login/", 
      data: { 
       options: { 
       username_or_email: username_or_email, 
       password: password 
       }, 
       context: {} 
      }.to_json, 
      module_path: "App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large)" 
      }) 
      set_type_converter -> (payload) {payload.to_json} 
      set_error_handler -> { 
      JSON.parse(body)['resource_response']['error'].to_s 
      } 
      puts post_body 
      post 
      @csrftoken = resp_cookies['csrftoken'] 
     end 
     end 
    end 
    end 
end 
+0

在你的控制器中放置一個斷點並檢查'params'的值。確保頂級密鑰在那裏。 –

+0

嗯,什麼?什麼控制器? – Thermatix

回答

0

好了,我發現問題,分析了同樣的事情JavaScript,但用PHP編寫的(link)我意識到,我被錯誤地發佈數據後,我需要發送的數據,而不是作爲JSON而是作爲查詢字符串(K = v & K = v)的變化:

set_type_converter -> (payload) {payload.to_json} 

set_type_converter -> (payload) {query_params(payload)} 

這導致post_body之中:

source_ury=%2Flogin%2F&data=%7B%22options%22%3A%7B%22username_or_email%22%3A%22testusername%40host.com%22%2C%22password%22%3A%22testpassword%22%7D%2C%22context%22%3A%7B%7D%7D&module_path=App%28%29%3ELoginPage%28%29%3ELogin%28%29%3EButton%28class_name%3Dprimary%2C+text%3DLog+In%2C+type%3Dsubmit%2C+size%3Dlarge%29 

這似乎已經解決了這個問題。