1

I am currently using Python perform a POST to a desired url and upload an in memory csv file:

Python code:

csv_content = 'some,fake,single,row,csv\r\n'
requests.post(
    'http://some.location.com',
    files={'form_field_name': ('file_name.csv', csv_content, 'text/csv')},
    # implicit "multipart/form-data" content-type header
)

The Python code works well, but I really want to use curl to perform the action.

What I have: (I know it is missing a lot, I tried variations of

curl -X POST http://some.location.com -H "Content-Type: text/csv"
  1. I am not sure the header is good
  2. Not sure how to specify the data, as -d would not be enough - I want to add a file name as well

1 Answer 1

1

How about this answer? There are several solutions for your situation, so please think of this as one of them.

When your python script is run, "form_field_name": "some,fake,single,row,csv\r\n" is sent as files. file_name.csv is used as the filename. In this case, the request body is as follows.

Request body:

--boundaryboundary
Content-Disposition: form-data; name="form_field_name"; filename="file_name.csv"
Content-Type: text/csv

some,fake,single,row,csv

--boundaryboundary--

Sample curl:

When above request body is used, the sample curl is as follows.

curl -H "Content-Type: multipart/form-data; boundary=boundaryboundary" \
  -d $'--boundaryboundary\r\nContent-Disposition: form-data; name="form_field_name"; filename="file_name.csv"\r\nContent-Type: text/csv\r\n\r\nsome,fake,single,row,csv\r\n\r\n--boundaryboundary--' \
  "http://some.location.com"
  • Content-Type of the header uses multipart/form-data; boundary=boundaryboundary.
  • The request body is directly used.
  • The filename is given as file_name.csv.
  • Each line break was replaced to \r\n.

If this was not the result you want, I apologize.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your time :) I tried using the curl you posted, used my real data, but it does not succeed
@camelBack Thank you for replying. I apologize for the inconvenience. About it does not succeed, can I ask you about the detail information and can you confirm your python script again? Because my sample curl is the same request body with your python script. I would like to confirm your situation and modify it.

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.