curl-and-python

Re: What is pycurl equivalent of this command?

From: Michael Wood <esiotrot_at_gmail.com>
Date: Fri, 2 Sep 2011 11:37:00 +0200

Hi

2011/9/2 Yaşar Arabacı <yasar11732_at_gmail.com>:
> Hi,
> I was trying to produce this call with pycurl, but couldn't succeed. I am
> getting an 400 error. What I am trying to do is:
>
> curl https://www.googleapis.com/urlshortener/v1/url \
> -H 'Content-Type: application/json' \
> -d '{"longUrl": "http://www.google.com/"}'
>
> What I did is:
> # defined api, url, json outputter here...
> c = pycurl.Curl()
> c.setopt(pycurl.URL,api)
> c.setopt(pycurl.POST,1)
> c.setopt(pycurl.HTTPHEADER, ["Content-Type: application/json"])
> c.setopt(pycurl.READFUNCTION, JsonOutputter(url).read)
> c.setopt(pycurl.SSL_VERIFYPEER,0)
> c.setopt(pycurl.SSL_VERIFYHOST,0)
> c.perfom()

You seem to have left out the longUrl stuff.

if you put "--libcurl /tmp/googleapiexample.c" at the end of the curl
command, it should generate a C file containing code to do what the
curl command does. You can use that as an example of what to do in
Python.

e.g. the C code contains this:

  curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{"longUrl":
"http://www.google.com/"}");

IIRC there's a nicer way to do this in pycurl. Check the
documentation for pycurl.POSTFIELDS.

-- 
Michael Wood <esiotrot_at_gmail.com>
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2011-09-02