我正在尝试与Amazon Cloud Backup RESTful API进行交互。为了删除服务器端的文件,我需要使用PUT请求。
下面的shell脚本效果很好:
curl -v -X PUT https://cdws.us-east-1.amazonaws.com/drive/v1/trash/161kKftTTTuRee1hicOhAw --header "Authorization: Bearer Atza|IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM"
但现在我想用libcURL来实现它。以下是我的代码:
int curlRequest(char *url, char *header){
CURL *curl;
CURLcode res;
struct curl_slist *chunk=NULL;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl=curl_easy_init();
if(curl){
chunk=curl_slist_append(chunk, header);
curl_easy_setopt(curl, CURLOPT_HTTPHEAD, chunk);
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res=curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(chunk);
}
Return 0;
}
然后我收到了如下回复,程序一直在等待:
< HTTP/1.1 100 Continue
我知道如果我使用PUT方法上传文件,我需要使用CURLOPT_READFUNCTION。但实际上我不需要上传任何数据。我该如何解决这个问题?
答案 0 :(得分:1)
我有同样的问题。你需要使用它:
public void ConfigureServices(IServiceCollection services)
services.AddMvc();
services.AddCors();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseIISPlatformHandler();
app.UseApplicationInsightsExceptionTelemetry();
app.StaticFiles();
app.UseCors(builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
而不是:
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");