473,372 Members | 1,506 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,372 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 4529
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.