0

I have a problem to send a POST request with a text file and some key=value (value contains special characters and it needs to be encoded, e.g: query=select c + a % b).

I've tried like this and it returns error Warning: You can only select one HTTP request!

curl -F "file=@path_to_text_file" 
--data-urlencode "query=select c + a % b" "http://localhost:8082/app"

remove --data-urlencode then it can run but the value of query is not encoded.

I prefer to not need to encode query manually.

Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50
  • Possible duplicate of [What is the right way to POST multipart/form-data using curl?](https://stackoverflow.com/questions/19116016/what-is-the-right-way-to-post-multipart-form-data-using-curl) – Dima Chubarov Feb 09 '18 at 16:49
  • @DmitriChubarov these topics only contain key=value which doesn't have the special characters. – Bằng Rikimaru Feb 09 '18 at 16:51
  • The accepted answer at https://stackoverflow.com/a/19148720/1328439 should solve it for you. To encode the query string use `urllib.urlencode()` – Dima Chubarov Feb 09 '18 at 16:56
  • @DmitriChubarov this is not what I wanted to use an external tool to encode, thanks though. – Bằng Rikimaru Feb 09 '18 at 17:00
  • 1
    You can specify exactly one content-type for one POST request. It can be either `application/x-www-form-urlencoded`, or `multipart/form-data` or `text/plain` but you have to use only one. For your command `curl` was complaining that you are trying to specify both `form-urlencoded` and `multipart/form-data`. – Dima Chubarov Feb 09 '18 at 17:07
  • this helps with your detail answer. I think there is no choice as I wanted. – Bằng Rikimaru Feb 09 '18 at 17:10

1 Answers1

1

This will do the upload then send the post request

curl -F "file=@path_to_text_file" "http://localhost:8082/app" --next --data-urlencode "query=select c + a % b" "http://localhost:8082/app"

LMC
  • 10,453
  • 2
  • 27
  • 52