api
  • python-3.x
  • ruby-on-rails-4
  • 2016-07-25 39 views 0 likes 
    0

    我遇到了Rails4/mongoid應用程序中的API問題。我需要通過一個API操縱數據與python 3腳本,但我得到NoMethodError(未定義的方法「許可」爲「note_id」:字符串):

    NoMethodError (undefined method `permit' for "note_id":String): 
    

    錯誤,當我嘗試提交請求。

    我的Python代碼看起來像這樣

    import requests 
    import json 
        url = 'http://0.0.0.0:3000/api/v1/note_proc_logs.json'  
        payload = {'note_proc_log' : { 'note_id' : '120904'}} 
        head = {"Authorization":"Token token=xxxxxxxxxxxxxxxxxxx"} 
        r = requests.post(url, payload, headers=head) 
    

    的API CONTROLER

    module Api 
        module V1 
        # This class does not inherit from ApplicationController like the rest to skip Devise authentication 
        class NoteProcLogsController < ActionController::Base 
         before_filter :restrict_access if Rails.env.development? == false 
    
         respond_to :json 
    
         def create 
         Rails.logger.warn "note_proc_log_params: #{params}" if Rails.env.development? 
    
         @note_proc_log = NoteProcLog.new(note_proc_log_params) 
    
         respond_to do |format| 
          if @note_proc_log.save 
          format.json { render :show, status: :created, location: @note_proc_log } 
          else 
          format.json { render json: @note_proc_log.errors, status: :unprocessable_entity } 
          end 
         end 
         end 
    
         private 
         def restrict_access 
          authenticate_or_request_with_http_token do |token, options| 
          ApiKey.where(access_token: token).exists? 
          end 
         end 
    
         # Never trust parameters from the scary internet, only allow the white list through. 
         def note_proc_log_params 
          params.require(:note_proc_log).permit(:note_id) 
         end 
        end 
        end 
    end 
    

    只見幾個問題有相同的錯誤,但無法找到一個解決我的問題。

    任何幫助將不勝感激。

    UPDATE:

    Rails.logger.warn "note_proc_log_params: #{params}" if Rails.env.development?

    給我

    W, [2016-07-25T15:10:38.362848 #48352] WARN -- : params: {"note_proc_log"=>"note_id", "format"=>"json", "controller"=>"api/v1/note_proc_logs", "action"=>"create"}

    +0

    你在發展嗎?你能發佈這一行的結果嗎? Rails.logger.warn「note_proc_log_params:#{params}」如果Rails.env.development? 我想看看參數的格式。 – fabriciofreitag

    +0

    我認爲你的有效載荷沒有爲參數生成適當的哈希 – hgsongra

    +0

    從你的有效載荷密鑰和冒號':'在你的Python代碼中刪除空間,這裏是新的paylod payload = {'note_proc_log':{'note_id':'120904 '}' – hgsongra

    回答

    0

    的問題是在python腳本。一個簡單的Python字典可以作爲有效載荷,但嵌套的字典似乎不是。

    我最後的Python腳本這個樣子

    import requests 
    import json 
    
    url = 'http://0.0.0.0:3000/api/v1/note_proc_logs.json' 
    payload='note_proc_log[chip_id]=120904&note_proc_log[test_timestamp]=2016-07-19T13:24:49' 
    head = {"Authorization":"Token token=xxxxxxxxxxxxxxxxxxxx"} 
    r = requests.post(url=url, data=payload, headers=head) 
    

    On Rails的側一切都將被視爲字符串,因此無需增加額外的引號,即使有空格的字符串,parent屬性必須爲每個被指定子屬性和元素用&分隔。

    這對我來說很有用,知道是否有其他更好的方法可以做到這一點很有意思,特別是如何包含一組值。

    相關問題