4

Curl error HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)

 1 year ago
source link: https://www.laravelcode.com/post/curl-error-http-2-stream-0-was-not-closed-cleanly-protocol-error-err-1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Curl error HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)

  1883 views

  11 months ago

PHP

When I was working with one curl request module in my PHP project, I was not getting response in few request. Instead I got error response "Curl error HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)" This was happening 4-5 times out of 10 attempt.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if (curl_error($ch)) {
    $error_msg = curl_error($ch);
}

curl_close($ch);

print_r($error_msg);

After a few research I found the issue and solution. New curl version set default HTTP/2 option CURL_HTTP_VERSION_2TLS. On older curl versions, default option was CURL_HTTP_VERSION_1_1

So older APIs services don't appear to be compatible with the latest HTTP version. So request with HTTP/2 protocol will fail to connect API. This results in confusing behaviour because a script that works on one machine may not necessarily work on another. To maximise compatibility, you should specify CURLOPT_HTTP_VERSION for curl requests to older servers which don't support CURL_HTTP_VERSION_2TLS.

In my case, I was using PHP programming language, so adding following line to curl request solved the issue:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

If you get same error in other language, find way to switch HTTP version HTTP/2 to HTTP/1.1

I hope this small article find you helpful.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK