3
curl --verbose google.com

If I execute this command, after curl does its job, it says "Connection #0 to host google.com left intact".

But it doesn't use same connection if I execute same command right after that. How can I make curl re-use same connection in second command?

Note: I don't want to use that command in below, because I have to do some bash scripting operations between two requests:

curl --verbose google.com --next google.com
1
  • I have the same problem. I need to perform a request, parse the response and make another request based on the response using the same connection otherwise the second request would fail. I tried to use a named pipe to make the curl wait before executing the second request. I used --next --data @mypipe for the second request (two request on the same command). However curl doesn't wait for the pipe to be filled and just report an error: Warning: Couldn't read data from file "mypipe", this makes an empty .
    – Bemipefe
    May 21 at 18:12
2

While searching around Google I found a couple of interesting answers and articles surrounding your question. Mainly the second answer given in https://stackoverflow.com/a/18047230/2901077 can help you understand not only how to use port reuse with libcurl but also when to use it.

Use the cURL_init function, implemented with PHP, to open a connection and the links includes reference's to the other two functions (curl_setopt & curl_exec) to be able to potentially reuse an existing handle (conncetion).

As a side note this answer, https://stackoverflow.com/a/982556/2901077 tells you how cURL handles new or existing requests. https://stackoverflow.com/a/15047105/2901077 is the answer though.

2
  • unfortunately I dont know C or PHP. Is it possible to do in command line? I have to use it in BASH
    – dandidondi
    Aug 22 '15 at 2:57
  • Well from what I understand if you reuse a handle you reuse the same connection. So take it for what it's worth. "serverfault.com/a/199446/284542"
    – gh0st
    Aug 22 '15 at 9:57
0

What about saving cookies in the first request and then reuse them in the second request.

This solved the problem for me. You can use

  • --dump-header mysession_cookies or --cookie-jar to save the cookies. The second one will merge the loaded cookies (if any) with the received ones.

  • --cookie mysession_cookies to load the cookies on the next requests

See more details in this discussion. So you are not using the same TCP session but from the HTTP point of view it looks like this.

If you want to reuse the same session TCP socket to send packets you need a specific software like haproxy which acts as mixer. That is it accepts your requests and keep an opened connection with the server. See this discussion for more details.

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.