【发布时间】:2022-01-07 08:53:45
【问题描述】:
我使用 curl 创建请求,但在标头中,curl 添加了“Transfer-Encoding”选项,但是,想要使用的 API 不知道此选项。我怎样才能删除这个选项?
【问题讨论】:
我使用 curl 创建请求,但在标头中,curl 添加了“Transfer-Encoding”选项,但是,想要使用的 API 不知道此选项。我怎样才能删除这个选项?
【问题讨论】:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_URL, end_point);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
buff = credential_scope;
buff = ACCESS_KEY + string("/") + buff;
printf("%s\n",buff.c_str());
string test_val = "";
char tampon[8];
for (int i = 0; i < SHA256_HASH_SIZE; i++)
{
sprintf(tampon,"%02x",signature[i]);
test_val += tampon;
}
struct data config;
config.trace_ascii = 1;
buffUser = (string) "Authorization:"+ ALGORITHM +" Credential="+ ACCESS_KEY + "/" + credential_scope + ",SignedHeaders=" + signed_header + ",Signature=" + test_val;
chunk = curl_slist_append(chunk,buffUser.c_str());
buffUser = "x-amz-content-sha256:" + payload_hash;
chunk = curl_slist_append(chunk,buffUser.c_str());
buff = amzdate;
buffUser = "x-amz-date:" + buff;
chunk = curl_slist_append(chunk,buffUser.c_str());
buffUser = "Content-Length: 1";
chunk = curl_slist_append(chunk,buffUser.c_str());
buffUser="Connection: keep-alive";
chunk = curl_slist_append(chunk,buffUser.c_str());
buffUser="Expect: 100-continue";
chunk = curl_slist_append(chunk,buffUser.c_str());
buffUser="Content-Type: text/plain";
chunk = curl_slist_append(chunk,buffUser.c_str());
/*buffUser = " Transfer-Encoding:-1";
chunk = curl_slist_append(chunk,buffUser.c_str());*/
curl_easy_setopt (curl,CURLOPT_HTTP_TRANSFER_DECODING,1);
curl_easy_setopt (curl, CURLOPT_DEBUGFUNCTION , my_trace);
curl_easy_setopt (curl, CURLOPT_DEBUGDATA , &config);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
//curl_easy_setopt(curl,CURLOPT_INFILESIZE,1);
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,chunk);
curl_easy_setopt(curl,CURLOPT_READDATA,fileOpen);
curl_easy_setopt(curl,CURLOPT_READFUNCTION,write_data);
curl_easy_setopt(curl,CURLOPT_INFILE,fileOpen);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
// }
fclose (fileOpen);
printf("Fin programme \n");
return 0;
我的代码和选项 curl_easy_setopt(curl, CURLOPT_PUT, 1);默认情况下,CURL 创建选项“Encoding-Content”。为了修复我的错误,我使用 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");它没有默认选项并修复我的错误。
【讨论】:
CURLOPT_READDATA 和 CURLOPT_INFILE 是相同的选项。如果您不想设置“Transfer-Encoding”标头,请告知 libcurl 文件大小:curl_easy_setopt(curl, CURLOPT_INFILESIZE, uploadsize),则不需要CURLOPT_CUSTOMREQUEST。 “Content-Length: 1”可能会使接收者感到困惑。
我用这条线解决了我的问题:
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
【讨论】: