Home > OS >  convert curl command to python
convert curl command to python

Time:05-19

I have below curl command

file_location='some/path/to/test.csv'

auth_token='my_api_token'

curl --insecure --request POST 'https://<ip_address>/eds/api/table/table_name/changes/addBulk?hasHeader=false' -H 'Csrf-Token: nocheck' -H 'AuthToken:'$auth_token'' -H 'Content-Type: multipart/form-data' --form 'data=@'$file_location'

The data in csv is just multiple rows with 1 column, for example below:

row_1
row_2
row_3

The curl command works perfectly fine, I am trying to get python alternative for it, I tried below:


files = {'file': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')}

auth_token="<api token here>"

url="https://"   ip_address  "/eds/api/table/"   table_name   "/changes/addBulk?hasHeader=false"

headers = {
    'Csrf-Token': 'nocheck',
    'AuthToken': auth_token
}

response = requests.post(url, headers=headers, files=files, verify=False)

Any help is appreciated

CodePudding user response:

I noticed in your curl url you have "eds" before "/api/..." but in your python version you do not.

curl_url = "https://<ip_address>/eds/api/table/table_name/changes/addBulk?hasHeader=false"

python_url = "https://"   ip_address  "/api/table/"   table_name   "/changes/addBulk?hasHeader=false"

Also, in python 3 it is cleaner to use an f-string like this:

url = f"https://{ip_address}**/eds/**api/table/{table_name}/changes/addBulk?hasHeader=false"

Everything else looks right to me. In the future it may help to set a break point on the response to see if there is any additional information with the 400 http response.

EDIT:

Okay then, can you try modifying your header to be:

headers = {
    'Csrf-Token': 'nocheck',
    'AuthToken': auth_token,
    'Content-Type': 'multipart/form-data'
}

Also edit your files to be:

files = {
    'data': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')
}

Final code should be:

auth_token = "<api token here>"
files = {
    'data': ('test.csv', open("some/path/to/test.csv", 'rb'), 'multipart/form-data')
}
url = f"https://{ip_address}/eds/api/table/{table_name}/changes/addBulk?hasHeader=false"

headers = {
    'Csrf-Token': 'nocheck',
    'AuthToken': auth_token
}

response = requests.post(url, headers=headers, files=files, verify=False)

CodePudding user response:

Use https://curlconverter.com/# for converting cURL to python's requests code.

  • Related