Curl to Python

Avatar

Avatar

BejoyMathews

Avatar

BejoyMathews

BejoyMathews

11-05-2018

My format is correct as per normal Python requests module rule, but still I am not able to upload a file to AEM using Python.

How do we convert the following

curl -u admin:admin -F file=@"C:\sample\samplepackage.zip" -F name="samplepackage" -F force=true -F install=false http://localhost:4502/crx/packmgr/service.jsp

to its Python equivalent??

This is what I used and it does not upload but gives me a 200 response code and says format is wrong

**************************************************************************************************************

#!/path-to-python-2.7.5/bin/python

import os

import requests

api_url = 'http://host:port/crx/packmgr/service.jsp'

os.chdir("/my/folder"); #change directory to where file.zip is located, and this I tested with subprocess listing and path is right

# Set up a session

session = requests.Session()

filehandle=open("file.zip",'rb')

payload = {}

payload['file'] = ('file.zip',filehandle)

payload['force'] = 'true'

payload['install'] = 'false'

payload['name'] = 'zipfile'

print payload

r = session.post(api_url, data=payload, files=payload, auth=('admin', '*******'))

print r.text

**************************************************************************************************************

What I get as output is the following which makes me think if something is missing in the requests module that AEM wants???

***********************

<crx version="1.6.2" user="admin" workspace="crx.default">

  <request>

    <param name="force" value="true"/>

    <param name="install" value="false"/>

    <param name="file" value="file.zip"/>

    <param name="name" value="zipfile"/>

  </request>

  <response>

    <data>

      +------------+-----------------------------------------+

      |  Arguments | Comment                                 |

      +------------+-----------------------------------------+

  ½   |  cmd=help  | print this help                         |

      +------------+-----------------------------------------+

      |  cmd=ls    | print a list of all packages            |

      +------------+-----------------------------------------+

      |  cmd=rm    | remove a  package                       |

      |  name      | package name                            |

      |  [group]   | group name (optional)                   |

      +------------+-----------------------------------------+

      |  cmd=build | build a  package                        |

      |  name      | package name                            |

      |  [group]   | group name (optional)                   |

      +------------+-----------------------------------------+

      |  cmd=inst  | install a package                       |

      |  name      | package name                            |

      |  [strict]  | true to fail on error                   |

      |  [group]   | group name (optional)                   |

      +------------+-----------------------------------------+

      |  cmd=uninst| uninstall a package                     |

      |  name      | package name                            |

      |  [group]   | group name (optional)                   |

      +------------+-----------------------------------------+

      |  GET       | download a package                      |

      |            | (content-disposition header contains    |

      |            | the correct filename)                   |

      |  [cmd=get] | optional                                |

      |  name      | package name                            |

      |  [group]   | group name (optional)                   |

      +------------+-----------------------------------------+

      |  POST      | upload a new package                    |

      |  file      | package to upload                       |

      |  [name]    | optional name                           |

      |  [strict]  | true to fail on install error           |

      |  [install] | automatically install package if 'true' |

      +------------+-----------------------------------------+

    </data>

    <status code="200">ok</status>

  </response>

</crx>

Accepted Solutions (0)

Answers (1)

Answers (1)

Avatar

Avatar

BejoyMathews

Avatar

BejoyMathews

BejoyMathews

12-05-2018

I got my answer - User Guide — urllib3 dev documentation  will do the trick, using multipart/form-data encoding. requests module was supposed to use urllib3 but doesnt seem to be, atleast for the way that is required for AEM.