curl-and-python

Re: How can I get the data into the put request

From: Michael Wood <esiotrot_at_gmail.com>
Date: Fri, 14 Oct 2011 17:53:36 +0200

On 14 October 2011 17:13, Paulina Gajewska <gajapaula_at_interia.pl> wrote:
> Hi List!
> How can I make this cURL example in pycurl?
> curl -u admin:geoserver -XPUT -H 'Content-type: text/xml' \
>  -d '<layer><defaultStyle><name>roads_style</name></defaultStyle></layer>' \
>  http://localhost:8080/geoserver/rest/layers/roads
>
> How can I get the data into the put request?

One thing that usually helps is to run your curl command with the
--libcurl option like this:

curl -u admin:geoserver -XPUT ... --libcurl example.c

then look at example.c to see what options are set and use the
corresponding ones in your Python code.

If you do that, you will see that the -XPUT is handled like this:

  curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "PUT");

and the data is included like this:

  curl_easy_setopt(hnd, CURLOPT_POSTFIELDS,
"<layer><defaultStyle><name>roads_style</name></defaultStyle></layer>");

> Here is my current code:
> import pycurl
> import cStringIO
> data ='<layer><defaultStyle><name>roads_style</name></defaultStyle></layer>'
> url = ‚http://localhost:8080/geoserver/rest/layers/roads%92
>
> response = cStringIO.StringIO()
>
> c = pycurl.Curl()
> c.setopt(c.POST, 1)

This option is not necessary if you use POSTFIELDS, since POSTFIELDS
implies POST according to the libcurl documentation, but it will not
hurt to have it here.

> c.setopt(c.HTTPHEADER, ['Content-type: text/xml'])
> c.setopt(c.USERPWD, "%s:%s" % ('admin','geoserver'))
> c.setopt(c.URL,url)
> c.setopt(c.WRITEFUNCTION, response.write)

Try adding something like this:

c.setopt(c.CUSTOMREQUEST, "PUT")
c.setopt(c.POSTFIELDS, data)

> c.perform()
>
> Thank you for your help
>
> Paulina

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