Search code examples
curlpython-requestsartifactory

Converting a curl to python requests


I want to convert my folowing CURL request to a python POST requests so I can use it with the requests library

curl -uadmin:AP31vzchw5mYTkB1u3DhjLT9Txj -T <PATH_TO_FILE> "http://MyArtifactory-Server/artifactory/OurRepo/<TARGET_FILE_PATH>"

Can anyone help in this case ?


Solution

  • The two aspects involved in your case is authentication and file uploading, you can refer to the links for more details. And also with the converted code below if you want it:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import requests
    from requests.auth import HTTPBasicAuth
    
    
    def upload_file():
        username = 'admin'
        password = 'AP31vzchw5mYTkB1u3DhjLT9Txj'
        source_file = "<your source file"
        upload_url = "http://<your server>/<your path>"
    
        files = {'file': open(source_file, 'rb')}
        requests.post(upload_url, auth=HTTPBasicAuth(username, password), files=files)
    
    if __name__ == "__main__":
        upload_file()
    

    Hope this helps:-)