0

I have the below curl command, I want to create an equivalent rest request using Python. How to do that ?

curl -i -X POST -u user:password --form details=@/user/home/services.json http://localhost:7001/api/v1.1/services

How to pass multiple headers & authorisation in the request. Also I would like to modify some of the fields in the json file.

For example if the services.json is like

{
"serviceName":"abc",
"id":1
}

I would like to modify the serviceName in the request I will be sending. How to achieve this ?

saurav
  • 359
  • 1
  • 6
  • 18

2 Answers2

3
import requests
requests.get('https://api.github.com/user', auth=('user', 'pass'))

Ref: https://2.python-requests.org/en/master/user/authentication/#basic-authentication

0

In order to send post request with data and headers, you can use requests library present for python. Here is an example inspired from requests module documentation.

payload = {"user": "user", "password": "password"}
headers = {'Authorization': 'YOUR AUTH HEADER', 'Content-Type', 'application/json'}
url = 'http://localhost:7001/api/v1.1/services'
r = requests.post(url, data= payload, headers=headers)

In case you want to modify the variable data you are sending send the variable itself like

variable = 'your variable data'
payload = {"user": "user", "password": "password", "id":1, "serviceName":variable}

now this will be your data that you will be sending in your post request. Reference1

Reference2

warl0ck
  • 3,356
  • 4
  • 27
  • 57