Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl does not terminate after successful POST

Tags:

bash

curl

I have created some curl command to send a POST to my server where I am listening on that port for input to trigger additional action. The command is the following (Just masked the URL):

curl -v -H "Content-Type: application/json" -X POST -d "{\"Location\":\"Some Name\",\"Value\":\"40%\"}" http://example.com:8885/

I get the following output from curl:

About to connect() to example.com port 8885 (#0)

Trying 5.147.XXX.XXX...

Connected to example.com (5.147.XXX.XXX) port 8885 (#0)

POST / HTTP/1.1

User-Agent: curl/7.29.0

Host: example.com:8885

Accept: /

Content-Type: application/json

Content-Length: 40

upload completely sent off: 40 out of 40 bytes

However after that curl does not close the connection. Am I doing something wrong? Also on the server I only receive the POST as soon as I hit ctrl+c.

like image 223
Nils Rehwald Avatar asked Aug 17 '17 14:08

Nils Rehwald


People also ask

How do I stop curl request?

The Curl client can tell the server that it wants to close the connection by sending the "Connection: close" header to the server. To pass the "Connection: close" header to Curl, you can use the -H command-line option.

How do you pass BODY IN POST request curl?

You can pass the body of the POST message to Curl with the -d or --data command-line option. Curl will send data to the server in the same format as the browser when submitting an HTML form. To send binary data in the body of a POST message with Curl, use the --data-binary command-line option.

Does Curl POST request?

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .

What does D mean in curl request?

The -d or --data option makes the curl command send data in POST request to the server. This option makes the curl command pass data to the server using content-type (JSON in your case) just as the browser does when a user submits a form.


2 Answers

It sits there waiting for the proper HTTP response, and after that has been received it will exit cleanly.

A minimal HTTP/1.1 response could look something like:

HTTP/1.1 200 OK
Content-Length: 0

... and it needs an extra CRLF after the last header to signal the end of headers.

like image 131
Daniel Stenberg Avatar answered Oct 05 '22 10:10

Daniel Stenberg


I'm a bit rusty on this, but according to section 6.1 of RFC7230, you might need to add a Connection: close header as well. Quoting part of the paragraph:

The "close" connection option is defined for a sender to signal that this connection will be closed after completion of the response. For example,

 Connection: close

in either the request or the response header fields indicates that the sender is going to close the connection after the current
request/response is complete (Section 6.6).

Let me know if it solves your issue :-)

like image 44
Polentino Avatar answered Oct 05 '22 10:10

Polentino