如何使用ASP.NET WebAPI接受cURL选项-T请求?
我正在尝试使用将文件发布到我的WebAPI如何使用ASP.NET WebAPI接受cURL选项-T请求?,curl,asp.net-web-api,azure-devops,azure-pipelines,Curl,Asp.net Web Api,Azure Devops,Azure Pipelines,我正在尝试使用将文件发布到我的WebAPI $ curl -F "appName=SomeApp" -F "version=1.0.0.0" -T C:/path/to/a/file.zip http://localhost/api/AppRepo/AddApp 按照这个答案,我已经能够接受一个带有选项-F“image=@C:/path/to/a/file.zip”的文件,并读取appName和version参数。但是,在VSTS构建任务中使用cURL时,我必须使用-T选项接受请求 到目前为止
$ curl -F "appName=SomeApp" -F "version=1.0.0.0" -T C:/path/to/a/file.zip http://localhost/api/AppRepo/AddApp
按照这个答案,我已经能够接受一个带有选项-F“image=@C:/path/to/a/file.zip”
的文件,并读取appName
和version
参数。但是,在VSTS构建任务中使用cURL时,我必须使用-T
选项接受请求
到目前为止,我发现它执行http PUT请求,并且内容不是MimeMultipart。我发现使用
-T
选项会阻止我将其他参数作为http内容发送,因此我添加了-H“X-App-Name:MyAppName”
和-H“X-App-Version:1.0.0”
在cURL调用中,将这些值作为HTTP头输入
我处理请求的WebAPI方法现在如下所示:
public async Task<HttpResponseMessage> PutApp()
{
const string UnexpectedContentOrHeadersMessage = "Expected a zip-file as content and http headers X-App-Name and X-App-Version.";
if (Request.Content.IsFormData() || Request.Content.IsHttpRequestMessageContent() || Request.Content.IsMimeMultipartContent())
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, UnexpectedContentOrHeadersMessage));
string appName;
IEnumerable<string> appNameValues;
if (Request.Headers.TryGetValues("X-App-Name", out appNameValues))
appName = appNameValues.First();
else
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, UnexpectedContentOrHeadersMessage));
Version version;
IEnumerable<string> appVersionValues;
if (Request.Headers.TryGetValues("X-App-Version", out appVersionValues))
version = new Version(appVersionValues.First());
else
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType, UnexpectedContentOrHeadersMessage));
return new HttpResponseMessage(HttpStatusCode.OK);
}
这是完整的cURL调用:
$curl--ntlm--user:-H“X-App-Name:”-H“X-App-Version:”-tc:/path/to/a/file.ziphttp://localhost/api/AppRepository/PutApp
var bytes = await httpContent.ReadAsByteArrayAsync();
Guid fileId = Guid.NewGuid();
FileInfo uploadFile = new FileInfo(Path.Combine(Server.MapPath("~/App_Data"), fileId.ToString()));
using (FileStream fileStream = uploadFile.OpenWrite())
{
await fileStream.WriteAsync(bytes, 0, bytes.Length);
}