Tastypie and python-requests

35 views
Skip to first unread message

David Fawcett

unread,
May 15, 2014, 10:50:56 AM5/15/14
to pym...@googlegroups.com
I have a Django-Tastypie app running and I can make successful calls to it through a browser (GET) and through curl (GET, PATCH).  The routes utilize Apikey authentication. 

What I can't figure out how to do correctly is make the same PATCH request via the Python Requests module.  The issue is how to pass the authentication type, username, and api_key. 

Here are a few of the examples that didn't work:

# in the headers
r = requests.patch("http://127.0.0.1:9000/api/v1/node-resources/24/", data=json.dumps({"update_needed":9}), headers={'Content-Type': 'application/json',"username":"MY_USER", "api_key":"MY_KEY"})

# in the data
payload = '{"update_needed": 5, "username":"MY_USER", "api_key":"MY_KEY"}'
r = requests.patch('http://127.0.0.1:9000/api/v1/node-resources/344/', data=payload, headers={'Content-Type': 'application/json'})

# including authorization type in header and user:key pair
#r = requests.patch('http://127.0.0.1:9000/api/v1/node-resources/344/', data='{"update_needed": 5}', headers={'Content-Type': 'application/json', 'Authorization':'ApiKey','MY_USER':MY_KEY'})

I have done quite a bit of searching around, and I am pretty surprised that I can't find any examples out there.

Any Requests ninjas out there?

Thanks,

David.

Kyle Marek-Spartz

unread,
May 15, 2014, 11:05:24 AM5/15/14
to pym...@googlegroups.com
I would try 

auth = (‘MY_USER’, ‘MY_KEY’)

r = requests.patch(…, auth=auth)

to start. If that doesn’t work, I would take a look at:


Have you tried it with HTTPie? (https://github.com/jakubroztocil/httpie) If you can get it to work with that, take a look at how they implement PATCH.

Kyle Marek-Spartz



On May 15, 2014, 9:50:56 AM, David Fawcett <david....@gmail.com> wrote:
--
Meetings Schedule / RVSP on our Meetup at http://python.mn
---
You received this message because you are subscribed to the Google Groups "PyMNtos" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymntos+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


David Fawcett

unread,
May 15, 2014, 3:38:02 PM5/15/14
to pym...@googlegroups.com
Kyle,

Thanks for the input.  Your first example still gives me a 401.  I will dig into HTTPie

David

Peter J. Farrell

unread,
May 15, 2014, 4:30:51 PM5/15/14
to pym...@googlegroups.com
What is the string that you are using in curl to test PATCH?  I've found curl to be a ninny when it comes to PATCH and if you don't have everything perfect -- it actually does a GET behind the scenes (because GET is the default).

Also, what does your is_authenticated() method look like in Tastypie? Are you sure you are allowing PATCH?

Have you tried adding this header workaround for clients that don't support PATCH right?

X-HTTP-Method-Override: PATCH

You'd just use a POST request and add the extra header to indicate to Tasty that it's really a PATCH request.

http://django-tastypie.readthedocs.org/en/latest/resources.html#using-put-delete-patch-in-unsupported-places

Have you tried to temporarily turn off auth to ensure it's not Python Requests but really an auth problem with PATCH in tastypie?

David Fawcett said the following on 05/15/2014 09:50 AM:
--
Meetings Schedule / RVSP on our Meetup at http://python.mn
---
You received this message because you are subscribed to the Google Groups "PyMNtos" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymntos+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
Peter J. Farrell
Principal Technologist - Maestro Publishing, LLC
http://blog.maestropublishing.com
Identi.ca / Twitter: @maestrofjp

* Learn about VSRE. I prioritize emails with VSRE in the subject!  http://vsre.info/
* Please do not send me Microsoft Office/Apple iWork documents. Send OpenDocument instead! http://fsf.org/campaigns/opendocument/

Peter J. Farrell

unread,
May 15, 2014, 4:38:20 PM5/15/14
to pym...@googlegroups.com
David Fawcett said the following on 05/15/2014 02:38 PM:
Kyle,

Thanks for the input.  Your first example still gives me a 401.  I will dig into HTTPie
What are you using for authentication in Tastypie?  Possibly?

from tastypie.authentication import SessionAuthentication

The reason I ask is SessionAuthentication requires a CRSF token for POST, PUT and PATCH methods.  Are PUT and POST working for you with authentication?  Just trying to narrow things down.

http://django-tastypie.readthedocs.org/en/latest/cookbook.html#using-sessionauthentication

If you can get it to work with curl and PATCH but not requests, I would suggest using Wireshark to see the actual packets being sent from Python Requests and compare it to curl's stuff...

David Fawcett

unread,
May 15, 2014, 10:29:04 PM5/15/14
to pym...@googlegroups.com
The request has worked with curl from the beginning, and I can do an ugly PATCH by including the username and api_key params as URL vars in the base URL of the PATCH request, but that just seems wrong. 

I am just updating one column/property.  e.g. {"update_needed":9}

I like the packet capture suggestion.  It is not a public IP, and I am not sure if I will be able to get wireshark installed on the server, but I will give it a shot.

David.


--

Peter J. Farrell

unread,
May 15, 2014, 11:11:18 PM5/15/14
to pym...@googlegroups.com
David Fawcett said the following on 05/15/2014 09:29 PM:
> Peter,
>
> Thanks for the great help. I am using
> http://django-tastypie.readthedocs.org/en/latest/authentication.html#apikeyauthentication
>
> The request has worked with curl from the beginning, and I can do an
> ugly PATCH by including the username and api_key params as URL vars in
> the base URL of the PATCH request, but that just seems wrong.
>
> I am just updating one column/property. e.g. {"update_needed":9}
>
> I like the packet capture suggestion. It is not a public IP, and I am
> not sure if I will be able to get wireshark installed on the server,
> but I will give it a shot.
You don't have to install wireshark on the server -- if you have it
installed on you local machine -- you can watch the requests you are
creating from curl and Python Requests locally.

David Fawcett

unread,
May 16, 2014, 9:19:06 AM5/16/14
to pym...@googlegroups.com
Peter,

Thanks.  I realize that Wireshark should go on the client machine.  Right now, I am testing from the shell on the box and from my work desktop.  Not sure if I will be able to put wireshark on my desktop either. 

Sounds like a very good strategy though.

David.


Nate Swanson

unread,
May 16, 2014, 11:38:08 AM5/16/14
to pym...@googlegroups.com
Bit of a late response, but if you're only doing HTTP, I really like MITMProxy. It's Python, too.  

Kyle Marek-Spartz

unread,
May 16, 2014, 2:10:16 PM5/16/14
to pym...@googlegroups.com, pym...@googlegroups.com
Nate,

Could you give a talk on mitmproxy at the web-dev or the main meeting? That would be great!

Kyle Marek-Spartz



On May 16, 2014, 10:38:08 AM, Nate Swanson <swanso...@gmail.com> wrote:
Bit of a late response, but if you're only doing HTTP, I really like MITMProxy. It's Python, too.  



Peter J. Farrell

unread,
May 16, 2014, 2:19:26 PM5/16/14
to pym...@googlegroups.com
Kyle Marek-Spartz said the following on 05/16/2014 01:10 PM:
Could you give a talk on mitmproxy at the web-dev or the main meeting? That would be great!
Ditto.  Great idea for a talk!

Nate Swanson

unread,
May 16, 2014, 2:35:12 PM5/16/14
to pym...@googlegroups.com
Sure. Any requests for targets? I'm thinking that something on iOS might be fun, since it's otherwise tough to see what's happening.

Peter J. Farrell

unread,
May 16, 2014, 2:36:21 PM5/16/14
to pym...@googlegroups.com
Nate Swanson said the following on 05/16/2014 01:35 PM:
Sure. Any requests for targets? I'm thinking that something on iOS might be fun, since it's otherwise tough to see what's happening.
iOS sounds ok to me...

Nathaniel Swanson

unread,
May 16, 2014, 2:39:51 PM5/16/14
to pym...@googlegroups.com
Ok, cool. I'll be out June 7-14, so the web dev meeting is the only one that works for me.


--

Peter J. Farrell

unread,
May 16, 2014, 2:58:55 PM5/16/14
to pym...@googlegroups.com
Consider yourself scheduled for the Web Dev meeting.

Nathaniel Swanson said the following on 05/16/2014 01:39 PM:

Tim Zehta

unread,
Jun 2, 2014, 10:39:45 AM6/2/14
to pym...@googlegroups.com
I find httpie much much easier to use than curl.

> HTTPie is a command line HTTP client, a human-friendly cURL replacement.

https://github.com/jakubroztocil/httpie

Peter J. Farrell

unread,
Jun 3, 2014, 2:19:47 PM6/3/14
to pym...@googlegroups.com
Fabulous suggestion Tim!

Tim Zehta said the following on 06/02/2014 09:39 AM:
> I find httpie much much easier to use than curl.
>
>> HTTPie is a command line HTTP client, a human-friendly cURL replacement.
>
> https://github.com/jakubroztocil/httpie
>


Kyle Marek-Spartz

unread,
Jun 3, 2014, 2:22:16 PM6/3/14
to pym...@googlegroups.com, pym...@googlegroups.com
See above:

"Have you tried it with HTTPie? (https://github.com/jakubroztocil/httpie) If you can get it to work with that, take a look at how they implement PATCH.

;)

Another HTTPie user.


Kyle Marek-Spartz



On Jun 3, 2014, 1:19:44 PM, Peter J. Farrell <p...@maestropublishing.com> wrote:
Fabulous suggestion Tim!

Tim Zehta said the following on 06/02/2014 09:39 AM:
> I find httpie much much easier to use than curl.
>
> > HTTPie is a command line HTTP client, a human-friendly cURL replacement.
>
> https://github.com/jakubroztocil/httpie
>


--
Peter J. Farrell
Principal Technologist - Maestro Publishing, LLC
http://blog.maestropublishing.com
http://Identi.ca / Twitter: @maestrofjp


* Learn about VSRE. I prioritize emails with VSRE in the subject! http://vsre.info/
* Please do not send me Microsoft Office/Apple iWork documents. Send OpenDocument instead! http://fsf.org/campaigns/opendocument/

--
Meetings Schedule / RVSP on our Meetup at http://python.mn
---
You received this message because you are subscribed to the Google Groups "PyMNtos" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mailto:pymntos+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages