0

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

4
  • Help with what? You seem to have already converted it. Commented May 18, 2022 at 16:25
  • But that is not working, unfortunately Commented May 18, 2022 at 16:25
  • Any error messages? Post them. Commented May 18, 2022 at 16:30
  • 1
    C:\Users\karan.nayak\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host '<ip address>'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings warnings.warn( <Response [400]> Commented May 18, 2022 at 16:37

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

11 Comments

Ah, actually that is my mistake in the post here, but my script does have the correct url... thanks for checking into this though I edited the post, now it should have eds
Can you try the suggestions in my edit.
Thanks for your help, i tried just now, but shows same error message
Sorry, I also made an additional change to your files. Did you try that as well?
Ohh, no, I didn't try that, let me do that now, thanks!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.