Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I post curl command as a python request?

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --header "Content-Type: application/json" \
  --data '{"path": "<subgroup_path>", "name": "<subgroup_name>", "parent_id": <parent_group_id> } \
  "https://gitlab.example.com/api/v4/groups/"

I was following the documentation from gitlab. I just wanted to to know how to represent the part after --data as a python request. Will it be a part of params, json or any other parameter in requests module?

Any help is appreciated. Thank you.

like image 899
Abhishek S Avatar asked Jul 25 '26 20:07

Abhishek S


2 Answers

Here's the equivalent using requests:

import requests
import json

headers = {
    "PRIVATE-TOKEN": "<your_access_token>",
    "Content-Type": "application/json",
}
data = {
    "path": "<subgroup_path>",
    "name": "<subgroup_name>",
    "parent_id": "<parent_group_id>",
}

requests.post("https://gitlab.example.com/api/v4/groups/",
    headers=headers, data=json.dumps(data))
like image 101
tzaman Avatar answered Jul 28 '26 09:07

tzaman


It can be done by python's requests package.

import requests
import json

url = "https://gitlab.example.com/api/v4/groups/"
headers = {'PRIVATE-TOKEN': '<your_access_token>', 'Content-Type':'application/json'}
data = {"path": "<subgroup_path>", "name": "<subgroup_name>", "parent_id": <parent_group_id>}

requests.post(url, data=json.dumps(data), headers=headers)

reference : Python Request Post with param data

like image 26
Prashant Godhani Avatar answered Jul 28 '26 10:07

Prashant Godhani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!