Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 947
Akki Avatar asked Mar 05 '17 04:03

Akki


People also ask

Does Python request use curl?

In Python, cURL transfers requests and data to and from servers using PycURL. PycURL functions as an interface for the libcURL library within Python. Almost every programming language can use REST APIs to access an endpoint hosted on a web server.

How do you change curl to postman?

Open POSTMAN. Click on "import" tab on the upper left side. Select the Raw Text option and paste your cURL command. Hit import and you will have the command in your Postman builder!

What are curl requests?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.


2 Answers

You have this utility:

https://curl.trillworks.com/

I use it all the time. An example is attached. enter image description here

like image 167
MouIdri Avatar answered Oct 24 '22 00:10

MouIdri


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:-)

like image 29
shizhz Avatar answered Oct 24 '22 00:10

shizhz