Updating records with Python 3 through API on Rails 4 application
Question: I'm working on a Rails 4 / mongoid application which needs to expose APIs for
other applications and scripts. I need to be able to update documents in one
of the models through an API with Python 3 script. I'm a bit fresh with Python
hence asking for help here.

I already found out how to query Rails APIs with Python 3 and urllib but
struggling with updates. I was trying to go through Python 3.5
[docs](https://docs.python.org/3/howto/urllib2.html) for urllib2 but
struggling to apply this to my script.

What goes to `data` and how to add authentication token to `headers`, which in
`curl` would look something like this

    
    
    -H 'Authorization:Token token="xxxxxxxxxxxxxxxx"'
    -X POST -d "name=A60E001&status=changed"
    

I would greatly appreciate if somebody explained how to, for example, update
`status` based on `name` (name is not unique yet but will be). My biggest
challenge is the Python side. Once I have the data in params on Rails side I
think I can handle it. I think.

I included my model and update action from the controller below.

`app/models/experiment.rb`

    
    
    class Experiment
      include Mongoid::Document
      include Mongoid::Timestamps
    
      field :name, type: String
      field :status, type:String
    end  
    

`app/controllers/api/v1/experiments_controller.rb`

    
    
    module Api
      module V1
        class ExperimentsController < ActionController::Base
          before_filter :restrict_access
    
          ...
    
          def update
            respond_to do |format|
              if @expt_proc.update(expt_proc_params)
                format.json { render :show, status: :ok, location: @expt_proc }
             else
                format.json { render json: @expt_proc.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
        ...
    


Answer: I figured out who to send a PATCH request with Python 3 and update the the
record's `status` by `name`.

Thanks to [this](http://stackoverflow.com/questions/6853050/how-do-i-make-a-
patch-request-in-python#answer-7112444) post found out about `requests`
module. I used [requests.patch](http://docs.python-
requests.org/en/master/user/quickstart/) to update the record and it works
great.

`python code`

    
    
    import requests
    import json
    
    url = 'http://0.0.0.0:3000/api/v1/update_expt_queue.json'
    payload = {'expt_name' : 'myExperiment1', 'status' : 'finished' }
    
    r = requests.patch(url, payload)
    

There are two problems remaining:

  1. How to add headers to this request which would allow me to go through token based authentication. At the moment it's disabled. [request.patch](http://docs.python-requests.org/en/master/api/#requests.patch) only takes 2 parameters, it doesn't take `headers` parameter.

  2. How to access the JSON which is sent in response by Rails API. 

