44

How can I pass content-length eg. --header Content-Length:1000 in curl command. I used it like this in command but it did't work

curl -v -X POST -u "xxx@gmail.com:xxx123" \ 
     --header "Content-Length: 1000" \
     --header "Content-Type: multipart/mixed" \
     --header "X-REQUEST-ID:7fc7d038-4306-4fc5-89c3-7ac8a12a30d0" \
     -F "request={\"bumId\":\"d51f2978-5ce8-4c71-8503-b0ca438741dd\",\"fileType\":\"imageFile\"};type=application/json" \
     -F "file=@D:/file.pdf" \
     "http://localhost:9090/pro/v1/files"

This command posts file to a web services developed in Jersey Java

3 Answers 3

44

You can use -d "" causes CURL to send a Content-Length: 0,

see Header 'Content-Length: 0' is missing when I do curl -X POST $URI

3
  • So whats the solution? remove the -d ? Jun 14, 2021 at 1:57
  • @WenhanXiao the original example is missing the -d "" which causes curl to compute the content-length Dec 6, 2021 at 2:00
  • see answer with -H for specific header values.
    – e-Fungus
    Dec 21, 2021 at 15:41
13

Try this: curl -X POST http://localhost:9090/pro/v1/files -d "Content-Length: 0"

curl's -d flag allows you to add data to the body of the request. According to its docs:

-d, --data (HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.

3
  • 5
    Welcome to Stack Overflow! I would suggest adding an explanation to your answer so that people with a similar problem are better able to understand the solution. Perhaps explain what the -d flag is? Maybe add a snippet from the Curl manual?
    – user1694691
    Jul 20, 2017 at 12:28
  • 4
    are you sure this is correct? -d is to specify the data to be posted Nov 27, 2018 at 13:38
  • Downvoted. -d is for request body. -H is for request header. By having -d "Content-Length: 0", you are sending the text as the request body.
    – blackr1234
    Jun 28, 2022 at 6:03
11

Adding headers like this should do the trick:

-H 'content-length: 1000' \

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.