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

51

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

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

3 Comments

So whats the solution? remove the -d ?
@WenhanXiao the original example is missing the -d "" which causes curl to compute the content-length
see answer with -H for specific header values.
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 ...

Comments

9

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 Comments

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?
are you sure this is correct? -d is to specify the data to be posted
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.

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.