Note that there are some explanatory texts on larger screens.

plurals
  1. POPOSTing a file and passing a parameter using pycurl
    text
    copied!<p>I have figured out how to do GET, PUTs, DELETEs and basic POST using pycurl.</p> <p>Nevertheless, I can't figure out what is the equivalent of this curl command line (which works perfectly fine) to python:</p> <pre><code>curl -u admin:geoserver -XPOST -H 'Content-type: application/vnd.ogc.sld+xml' -d @/Users/rburhum/src/calthorpe/calthorpe/server/calthorpe/media/styles/1_my-scenario "http://127.0.0.1:8080/geoserver/rest/styles?name=1_my-scenario" -v </code></pre> <p><a href="http://pycurl.cvs.sourceforge.net/viewvc/pycurl/pycurl/tests/test_post.py?revision=1.9&amp;view=markup" rel="nofollow">I have seen the samples at the repo</a>. Nevertheless, passing one parameter (in this case <em>name</em>) and a file to be uploaded doesn't seem to work.</p> <p>For PUT calls, I have successfully used:</p> <pre><code>filesize = path.getsize(sldFile) f = open(sldFile,'rb') c = pycurl.Curl() c.setopt(pycurl.HTTPHEADER, ["Content-type: application/vnd.ogc.sld+xml"]) c.setopt(pycurl.USERPWD, GEOSERVER_USER + ':' + GEOSERVER_PASSWORD) c.setopt(pycurl.INFILESIZE, filesize) c.setopt(c.URL, str(GEOSERVER_URL + '/rest/styles/' + path.basename(sldFile))) c.setopt(pycurl.PUT, 1) c.setopt(pycurl.INFILE, f) c.perform() f.close() </code></pre> <p>thus, I naively thought that the POST equivalent would be:</p> <pre><code>filesize = path.getsize(sldFile) f = open(sldFile,'rb') c = pycurl.Curl() c.setopt(c.URL, str(GEOSERVER_URL + '/rest/styles?name=' + path.basename(sldFile))) c.setopt(pycurl.POST, 1) c.setopt(pycurl.HTTPHEADER, ["Content-type: application/vnd.ogc.sld+xml"]) c.setopt(pycurl.USERPWD, GEOSERVER_USER + ':' + GEOSERVER_PASSWORD) c.setopt(pycurl.INFILESIZE, filesize) c.setopt(pycurl.INFILE, f) c.perform() f.close() </code></pre> <p>The verbose output of the curl command displays this:</p> <p>(calthorpe_env)rburhum@peru.local ~/src/calthorpe/calthorpe/server/calthorpe $calthorpe/server/calthorpe POST -H 'Content-type: application/vnd.ogc.sld+xml' -d@/Users/rburhum/src/calthorpe/calthorpe/server/calthorpe/media/styles/1_my-scenario "http://127.0.0.1:8080/geoserver/rest/styles?name=1_my-scenario" -v * About to connect() to 127.0.0.1 port 8080 (#0) * Trying 127.0.0.1... connected * Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0) * Server auth using Basic with user 'admin'</p> <blockquote> <p>POST /geoserver/rest/styles?name=1_my-scenario HTTP/1.1 Authorization: Basic YWRtaW46Z2Vvc2VydmVy User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 Host: 127.0.0.1:8080 Accept: <em>/</em> Content-type: application/vnd.ogc.sld+xml Content-Length: 28135 Expect: 100-continue</p> </blockquote> <p>  > &lt; HTTP/1.1 100 Continue &lt; HTTP/1.1 201 Created &lt; Date: Mon, 05 Mar 2012 07:39:43 GMT &lt; Location: <a href="http://127.0.0.1:8080/geoserver/rest/styles/1_my-scenario" rel="nofollow">http://127.0.0.1:8080/geoserver/rest/styles/1_my-scenario</a> &lt; Server: Noelios-Restlet-Engine/1.0..8 &lt; Transfer-Encoding: chunked &lt; * Connection #0 to host 127.0.0.1 left intact * Closing connection #0</p> <p>I can clearly see that the content length is correct.</p> <p>In contrast, when I do a verbose output of my POST code above, I can see that the content length is -1 (and hence the file is not being passed and the server gives back a 500). I have mocked with the HTTPPOST variable, which under certain combinations makes the content length be correct, but I still cannot get the exact equivalent of the CURL command above to work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload