Tag: restsharp

使用RestSharp发送HTTP POST Multipart / form-data字段

我在使用RestSharp进行REST API时遇到问题,我需要将其用于我正在进行的项目。 我需要发出的请求分为三部分:头API密钥,要上传的文件和一堆JSON格式的数据。 API要求使用表单字段名称“data”发送数据部分。 出于某种原因,这会导致问题,因为它在请求正文中命名字段“data”。 我的代码如下: var request = new RestRequest(UPLOAD_DOC_URLSEGMENT, Method.POST) { RequestFormat = DataFormat.Json, AlwaysMultipartFormData = true, JsonSerializer = new RestSharpJsonDotNetSerializer(customSerializer) }; if (doc is DocA) request.AddParameter(“data”,doc as DocA,ParameterType.RequestBody); //request.AddBody(doc as DocA); else request.AddParameter(“data”, doc as DocB,ParameterType.RequestBody); //request.AddBody(doc as DocB); request.AddFile(“file”, doc.File.FullName); 如您所见,我试图同时使用request.AddBody(doc)方法和request.AddParameter(name, object, type)方法。 它们似乎都没有正确发送数据,因为我收到服务器的响应,说明缺少必需的参数。 使用fiddler我可以看到二进制数据,但从来没有使用这两种方法的JSON数据。 我已经浏览了RestSharp文档,但我找不到任何允许我为表单数据体指定特定“字段”名称作为“数据”的内容,这是我认为导致我遇到的问题。 我在这做错了什么? 编辑:进一步检查fiddler后,它似乎没有将我的JSON数据添加到HTTP请求的主体。 但是,在上传(执行命令)之前有一个断点,我可以在参数列表(和文件列表)中看到所有序列化的内容。 当用Fiddler检查时,我看到文件二进制数据,然后是多部分/表格数据边界,然后什么都没有。 我认为这是我的数据应该是…

使用RestSharp设置’Content-Type’标头

我正在为RSS阅读服务建立一个客户端。 我正在使用RestSharp库与他们的API进行交互。 API声明: 创建或更新记录时,必须设置application / json; charset = utf-8作为Content-Type标头。 这就是我的代码: RestRequest request = new RestRequest(“/v2/starred_entries.json”, Method.POST); request.AddHeader(“Content-Type”, “application/json; charset=utf-8”); request.RequestFormat = DataFormat.Json; request.AddParameter(“starred_entries”, id); //Pass the request to the RestSharp client Messagebox.Show(rest.ExecuteAsPost(request, “POST”).Content); 然而; 服务返回“错误415:请使用’Content-Type:application / json; charset = utf-8’标题。” 为什么RestSharp不通过标题?

RestSharp可以在不使用多部分内容类型的情况下发送二进制数据吗?

我一直在使用AddParameter在我的HTTP请求中包含XML主体: request.AddParameter(contentType, body, ParameterType.RequestBody); 但是,这似乎不适用于非弦体。 (RestSharp的Http.RequestBody由于某种原因是一个字符串。)我尝试使用AddFile() ,但我找不到任何方法来避免将“文件”编码为multipart / form ,即使我只提供了一个对象。 我并不反对解决这个问题的反思,但我希望避免修改源只是为了在HTTP请求中发送任意数据。 编辑:关于我要发送的请求,它们看起来像这样: PUT … HTTP/1.1 Accept: application/vnd… Authorization: Basic … Content-Type: application/octet-stream 理想情况下,我想使用相同的调用来发送不同的内容类型: PUT … HTTP/1.1 Accept: application/vnd… Authorization: Basic … Content-Type: application/vnd…

我应该如何在Windows Phone 7上使用RestSharp实现ExecuteAsync?

我正在尝试使用RestSharp GitHub wiki上的文档来实现对我的REST API服务的调用,但特别是我遇到了ExecuteAsync方法的问题。 目前我的代码对于API类来说是这样的: public class HarooApi { const string BaseUrl = “https://domain.here”; readonly string _accountSid; readonly string _secretKey; public HarooApi(string accountSid, string secretKey) { _accountSid = accountSid; _secretKey = secretKey; } public T Execute(RestRequest request) where T : new() { var client = new RestClient(); client.BaseUrl = BaseUrl; client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey); […]

使用动态对象反序列化JSON

我有一个JSON对象,附带一长串区号。 不幸的是,每个区域代码都是Data对象列表中的对象名称。 如何创建一个允许RestSharp反序列化内容的类? 这是我class级现在的样子: public class phaxioResponse { public string success { get; set; } public string message { get; set; } public List data { get; set; } public class areaCode { public string city { get; set; } public string state { get; set; } } } 这是JSON内容: { success: true message: “277 […]

如何使用RestSharp发送请求

我试图使用RestSharp客户端POST请求,如下所示我将Auth Code传递给以下函数 public void ExchangeCodeForToken(string code) { if (string.IsNullOrEmpty(code)) { OnAuthenticationFailed(); } else { var request = new RestRequest(this.TokenEndPoint, Method.POST); request.AddParameter(“code”, code); request.AddParameter(“client_id”, this.ClientId); request.AddParameter(“client_secret”, this.Secret); request.AddParameter(“redirect_uri”, “urn:ietf:wg:oauth:2.0:oob”); request.AddParameter(“grant_type”, “authorization_code”); request.AddHeader(“content-type”, “application/x-www-form-urlencoded”); client.ExecuteAsync(request, GetAccessToken); } } void GetAccessToken(IRestResponse response) { if (response == null || response.StatusCode != HttpStatusCode.OK || response.Data == null || string.IsNullOrEmpty(response.Data.access_token)) { […]