Francisco Costa
Francisco Costa

Reputation: 7133

Translate CURL command to PyCurl

I'm struggling to translate this command (that I can successfully run)

curl -X POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: multipart/related" \
-F "[email protected];type=application/json;charset=UTF-8" \
-F "[email protected];type=image/jpeg"

to a python script using pycurl:

import pycurl
url = 'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart'

c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.USERAGENT, 'Curl')
c.setopt(c.VERBOSE, 1)
c.setopt(c.URL, url)
c.setopt(c.HTTPHEADER, [
    'Authorization: Bearer %s' % str(ACCESS_TOKEN),
    'Content-Type: multipart/related',
])

data = [
     ('metadata', (c.FORM_FILE, 'metadata.txt')),
     ('type', 'application/json'),
     ('charset', 'UTF-8'),
     ('file', (c.FORM_FILE, 'upload.jpg')),
     ('type', 'image/jpeg'),
]
c.setopt(c.HTTPPOST, data)
c.perform()
c.close()

but I always get this error when i run the script

* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 74.125.132.95... * connected
* found 153 certificates in /etc/ssl/certs/ca-certificates.crt
*    server certificate verification OK
*    common name: *.googleapis.com (matched)
*    server certificate expiration date OK
*    server certificate activation date OK
*    certificate public key: RSA
*    certificate version: #3
*    subject: C=US,ST=California,L=Mountain View,O=Google Inc,CN=*.googleapis.com
*    start date: Thu, 27 Sep 2012 01:23:09 GMT
*    expire date: Fri, 07 Jun 2013 19:43:27 GMT
*    issuer: C=US,O=Google Inc,CN=Google Internet Authority
*    compression: NULL
*    cipher: ARCFOUR-128
*    MAC: SHA1
> POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
User-Agent: Curl
Host: www.googleapis.com
Accept: */*
Authorization: Bearer ACCESS_TOKEN
Content-Length: 43373
Expect: 100-continue
Content-Type: multipart/related; boundary=----------------------------b91f88a180be

* Done waiting for 100-continue
< HTTP/1.1 400 Bad Request
< Server: HTTP Upload Server Built on Sep 26 2012 16:51:11 (1348703471)
< Content-Type: application/json
< Date: Fri, 05 Oct 2012 15:20:26 GMT
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Length: 171
* HTTP error before end of send, stop sending
< 
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}
* Closing connection #0

Do you know the right translation?

I've removed the access token of the code examples for security reasons

Upvotes: 1

Views: 2442

Answers (1)

Francisco Costa
Francisco Costa

Reputation: 7133

just found the solution

data values must be this way:

data = [
    ('metadata', (
        c.FORM_FILE, 'metadata.txt', 
        c.FORM_CONTENTTYPE, 'application/json')),
    ('file', (
        c.FORM_FILE, 'upload.jpg', 
        c.FORM_CONTENTTYPE, 'image/jpeg'))
]

Upvotes: 2

Related Questions