0

I created an HTTP listener that accepts POST requests for files. I followed this template almost exactly: https://github.com/cpp-netlib/cpp-netlib/blob/main/libs/network/example/http/echo_async_server.cpp

I start my listener like so: ./build/http_listener 0.0.0.0 8000

Then post file to the listener using curl: curl --form "fileupload=@file1.txt" -X POST http://127.0.0.1:8000/

I noticed that the variable body__, which gets populated with the text of the file also gets populated with header data. I do not want this. How do I populate the variable body__ without any header data?

This is what it looks like:

enter image description here

1 Answers1

1

body__ does not get populated with header data. What you see is multipart data that you submit with --form <name=content>. If you want submit raw file data use another curl command:

curl --data-binary "@file1.txt"  -H "Content-Type: application/octet-stream" http://127.0.0.1:8000/

-X POST can be omitted if is used with --data-binary, --data, --form.

Unfortunately curl does not send a file name. If a file name is required on a server side, specify the header Content-Disposition:

curl --data-binary "@file1.txt"  -H "Content-Type: application/octet-stream" -H "Content-Disposition: attachment; filename=file1.txt" http://127.0.0.1:8000/

KB

  1. --data-binary
  2. -F, --form
273K
  • 29,503
  • 10
  • 41
  • 64
  • wow, you are a life-saver thank you so much! This worked for me. Do you know how the HTTP listener can retrieve the name of the original file if a header is not passed? I want the HTTP listener to store the name of the original file inside a variable. I want to use this variable for various purposes. – average_coder25 Mar 27 '22 at 21:46
  • See the update. – 273K Mar 27 '22 at 22:23
  • thank you!! I appreciate your help. My program works perfectly now – average_coder25 Mar 27 '22 at 22:43