47

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 "[email protected]: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

48

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 ? Commented Jun 14, 2021 at 1:57
  • 1
    @WenhanXiao the original example is missing the -d "" which causes curl to compute the content-length Commented Dec 6, 2021 at 2:00
  • 1
    see answer with -H for specific header values.
    – e-Fungus
    Commented Dec 21, 2021 at 15:41
13

Adding a header like this should do the trick:

-H 'content-length: 1000'

Full example:

$ curl -v -XPOST -H 'content-length: 1000' https://example.com
# ... snip ...
> POST / HTTP/2
> Host: example.com
> User-Agent: curl/8.5.0
> Accept: */*
> content-length: 1000
>
# ... snip ...
11

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
    Commented Jul 20, 2017 at 12:28
  • 6
    are you sure this is correct? -d is to specify the data to be posted Commented Nov 27, 2018 at 13:38
  • 1
    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
    Commented Jun 28, 2022 at 6:03

Your Answer

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

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