473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

urllib2 POST problem

I'm struggling with using urllib2 to access the Harvest time-tracking
web service (http://www.getharvest.com/api). GET is working fine.
POST is giving me a problem. Here is an example that creates a new
time-tracking entry using curl.

$ curl http://subdomain.harvestapp.com/daily/add -H 'Accept:
application/xml' \
-H 'Content-Type: application/xml' \
-u myusername:mypassword \
-d '<request><notes>testing API</notes><hours>3.5</
hours><project_id>65750</project_id><task_id>79702</
task_id><spent_at>Thu, 13 Sep 2007</spent_at></request>'

Here's a selection of info from running the above with --verbose
output:

* About to connect() to edmstudio.harvestapp.com port 80
POST /daily/add HTTP/1.1
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Accept: application/xml
Content-Type: application/xml
Content-Length: 154

<request><notes>testing API</notes><hours>3.5</hours><project_id>65750</project_id><task_id>79702</task_id><spent_at>Thu, 13 Sep 2007</spent_at></request>HTTP/1.1 201 Created
Notice that 1) it is a POST, and 2) the data is going across as an
unencoded (xml) string. Now here is my attempt to achieve this with
python's urllib2:

entry = "<request><notes>testing API</notes><hours>3.5</
hours><project_id>65750</project_id><task_id>79702</
task_id><spent_at>Thu, 13 Sep 2007</spent_at></request>"

import urllib2
import base64
opener = urllib2.build_opener()
opener.addheaders = [
('Content-Type', 'application/xml'),
('Accept', 'application/xml'),
('Authorization', 'Basic %s' \
% base64.encodestring('%s:%s' % (myusername, mypassword)))]

# this GET works just fine - proof that authentication is correct
req = urllib2.Request(url='http://subdomain.harvestapp.com/daily')
response = opener.open(req)
print response.read()

# this POST (same data as the above curl example) fails with an
internal server error (500)
req = urllib2.Request(url='http://subdomain.harvestapp.com/daily/add',
data=entry)
response = opener.open(req)
print response.read()

I'm stumped. As always, any help is much appreciated.

Sep 19 '07 #1
1 4533
In message <11*********************@v29g2000prd.googlegroups. com>, darran
wrote:
entry = "<request><notes>testing API</notes><hours>3.5</
hours><project_id>65750</project_id><task_id>79702</
task_id><spent_at>Thu, 13 Sep 2007</spent_at></request>"
....
# this POST (same data as the above curl example) fails with an
internal server error (500)
req = urllib2.Request(url='http://subdomain.harvestapp.com/daily/add',
data=entry)
response = opener.open(req)
print response.read()
Perhaps the value of "entry" needs to be URL-encoded?
Sep 21 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using...
1
by: Doug Farrell | last post by:
Hi all, I'm trying to build a web page crawler to help us build our websites, which are driven by static pages after they are called the first time. Anyway, I can use urllib2.urlopen() no...
1
by: Herman Geldenhuys | last post by:
Hey. Im trying to post some variables to an URL, but I get an error saying that my XML is parsed incorrectly. I know that it is not. At closer inspection I found out that some module in Python...
1
by: kelio | last post by:
I have a simple cgi-script on a server that prints all key-value pairs from a request. And it really works when i use a browser and type smth like http://server/cgi-bin/test?name=mike&johny=dummy....
1
by: tomazi75-nospam(at)gmail.com | last post by:
Hello all, I've a problem using urllib2 with a proxy which need authentication. I've tested the 'simple way' : -- code -- import urllib # example values for the post
2
by: Ant | last post by:
Hi all, I have just moved to a new machine, and so have installed the latest version of Python (2.4.3 - previously I believe I was running 2.4.2). Unfortunately this seems to have broken...
2
by: Joshua Kugler | last post by:
Standard disclaimer: read, googled, read some more. If you have a link, please free free to point me there. I'm using HTTPlib to construct some functional tests for a web app we're writing. ...
3
by: Alexnb | last post by:
Okay, I tried to follow that, and it is kinda hard. But since you obviously know what you are doing, where did you learn this? Or where can I learn this? Maric Michaud wrote:...
1
by: Larry Hale | last post by:
Since it seems I have a "unique" problem, I wonder if anyone could point me in the general/right direction for tracking down the issue and resolving it myself. See my prior post @...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.