gpt4 book ai didi

restsharp - 使用 RestSharp 上传没有 Multipart/Form-Data 的文件

转载 作者:行者123 更新时间:2023-12-03 13:55:23 25 4
gpt4 key购买 nike

下面是使用标准 HttpWebRequest 和 HttpWebResponse 完成的 POST 请求。基本上它发布带有一些参数的二进制文件。

我怎样才能用 RestSharp 做同样的事情?

来源:

https://github.com/attdevsupport/ATT_APIPlatform_SampleApps/tree/master/RESTFul/SpeechCustom , ATT_APIPlatform_SampleApps/RESTFul/Speech/Csharp/app1/]

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(string.Empty+parEndPoint );

httpRequest.Headers.Add("Authorization", "Bearer " + parAccessToken);
httpRequest.Headers.Add("X-SpeechContext", parXspeechContext);
if (!string.IsNullOrEmpty(parXArgs))
{
httpRequest.Headers.Add("X-Arg", parXArgs);
}
string contentType = this.MapContentTypeFromExtension(Path.GetExtension(parSpeechFilePath));
httpRequest.ContentLength = binaryData.Length;
httpRequest.ContentType = contentType;
httpRequest.Accept = "application/json";
httpRequest.Method = "POST";
httpRequest.KeepAlive = true;
httpRequest.SendChunked = parChunked;
postStream = httpRequest.GetRequestStream();
postStream.Write(binaryData, 0, binaryData.Length);
postStream.Close();

HttpWebResponse speechResponse = (HttpWebResponse)httpRequest.GetResponse();

最佳答案

一个简单的上传示例:

RestClient restClient = new RestClient("http://stackoverflow.com/");
RestRequest restRequest = new RestRequest("/images");
restRequest.RequestFormat = DataFormat.Json;
restRequest.Method = Method.POST;
restRequest.AddHeader("Authorization", "Authorization");
restRequest.AddHeader("Content-Type", "multipart/form-data");
restRequest.AddFile("content", imageFullPath);
var response = restClient.Execute(restRequest);

关于restsharp - 使用 RestSharp 上传没有 Multipart/Form-Data 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com