标签: restsharp

是否可以上传文件以及使用servicestack发布数据?

我希望能够发布一个文件,并作为该帖子的一部分添加数据.

这是我有的:

            var restRequest = new RestRequest(Method.POST);

            restRequest.Resource = "some-resource";
            restRequest.RequestFormat = DataFormat.Json;

            string request = JsonConvert.SerializeObject(model);
            restRequest.AddParameter("text/json", request, ParameterType.RequestBody);

            var fileModel = model as IHaveFileUrl;

            var bytes = File.ReadAllBytes(fileModel.LocalStoreUrl);

            restRequest.AddFile("FileData", bytes, "file.zip", "application/zip");


            var async = RestClient.ExecuteAsync(restRequest, response =>
            {
                if (PostComplete != null)
                    PostComplete.Invoke(
                        new Object(),
                        new GotResponseEventArgs
                            <T>(response));
            });
Run Code Online (Sandbox Code Playgroud)

它发布文件很好,但数据不存在 - 这甚至可能吗?

[UPDATE]

我修改了代码以使用多部分标题:

            var restRequest = new RestRequest(Method.POST);

            Type t = GetType();
            Type g = t.GetGenericArguments()[0];

            restRequest.Resource = string.Format("/{0}", g.Name);
            restRequest.RequestFormat = DataFormat.Json;
            restRequest.AddHeader("content-type", "multipart/form-data");

            string …
Run Code Online (Sandbox Code Playgroud)

c# rest restsharp servicestack

10
推荐指数
1
解决办法
4238
查看次数

如何使用Restsharp反序列化Xml列表?

我有这样的xml

<?xml version="1.0" encoding="utf-8"?> 
    <xml> 
            <item> 
                    <accountid>1</accountid> 
                    <accounttypeid>1</accounttypeid> 
                    <accounttypename/> 
                    <accountbankid>1</accountbankid> 
                    <accountbankname/> 
                    <accountsaldo>0</accountsaldo> 
            </item> 
            <item> 
                    <accountid>2</accountid> 
                    <accounttypeid>1</accounttypeid> 
                    <accounttypename/> 
                    <accountbankid>2</accountbankid> 
                    <accountbankname/> 
                    <accountsaldo>0</accountsaldo> 
            </item> 
            ... 
    </xml> 
Run Code Online (Sandbox Code Playgroud)

我想将这个xml列表反序列化为POCO对象

public class Account 
{ 
        public string AccountId { get; set; } 
        public string AccountTypeId { get; set; } 
        public string AccountTypeName { get; set; } 
        public string AccountBankId { get; set; } 
        public string AccountBankName { get; set; } 
        public string AccountSaldo { get; set; } 
} 
Run Code Online (Sandbox Code Playgroud)

我找到了很棒的产品RestSharp与rest客户端一起工作.我想使用它的解串器,我尝试了两种方法.

1)我试过了

request.RootElement = "item";

var response …

c# rest restsharp

9
推荐指数
2
解决办法
6791
查看次数

如何在没有NuGet的情况下安装和设置RESTSharp?

我正在使用Visual Studio C#2010 Express并正在学习.RESTSharp应该由NuGet安装,但NuGet不能与Express版本一起使用.我还可以手动安装和配置RESTSharp吗?

.net c# restsharp

9
推荐指数
2
解决办法
2万
查看次数

如何使用RestSharp解析JSON?

var client = new RestClient("http://10.0.2.2:50670/api");

var request = new RestRequest("Inventory", Method.GET);

request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

// execute the request to return a list of InventoryItem
RestResponse<JavaList<InventoryItem>> response = (RestResponse<JavaList<InventoryItem>>)client.Execute<JavaList<InventoryItem>>(request);
Run Code Online (Sandbox Code Playgroud)

返回的内容是JSON字符串,即对象数组.以下是它的简短摘录:

[{"Id":1,"Upc":"1234567890","Quantity":100,"Created":"2012-01-01T00:00:00","Category":"Tequila","TransactionType":"Audit","MetaData":"PATRON 750ML"},{"Id":2,"Upc":"2345678901","Quantity":110,"Created":"2012-01-01T00:00:00","Category":"Whiskey","TransactionType":"Audit","MetaData":"JACK DANIELS 750ML"},{"Id":3,"Upc":"3456789012","Quantity":150,"Created":"2012-01-01T00:00:00","Category":"Vodka","TransactionType":"Audit","MetaData":"ABSOLUT 750ml"}]
Run Code Online (Sandbox Code Playgroud)

错误消息:

由于对象的当前状态,操作无效

这有什么不对?我InventoryItem的属性与JSON字符串中的每个对象相同.我错过了一步吗?

c# restsharp xamarin.android

9
推荐指数
1
解决办法
2万
查看次数

RestSharp可以在POST请求中发送List <string>吗?

我正在努力让RestSharp与我拥有的宁静服务一起工作.一切似乎工作得很好,除非我传递的对象POST包含一个列表(在这个特殊情况下是一个列表string).

我的目标:

public class TestObj
{
    public string Name{get;set;}
    public List<string> Children{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

当它被发送到服务器时,Children属性将作为包含内容的字符串发送System.Collections.Generic.List`1[System.String].

这是我发送对象的方式:

var client = new RestClient();
var request = new RestRequest("http://localhost", Method.PUT);

var test = new TestObj {Name = "Fred", Children = new List<string> {"Arthur", "Betty"}};
request.AddObject(test);
client.Execute<TestObj>(request);
Run Code Online (Sandbox Code Playgroud)

我做错了什么,或者这是RestSharp中的错误?(如果它有所不同,我使用的是JSON,而不是XML.)

serialization json restsharp

9
推荐指数
2
解决办法
9101
查看次数

我错过了什么?RestSharp不会反序列化Json

我试图将json响应从foursquare变回对象.我得到这样的东西了

   {
   "meta":{
      "code":200
   },
   "response":{
      "venues":[
         {
            "id":"4abfb58ef964a520be9120e3",
            "name":"Costco",
            "contact":{
               "phone":"6045967435",
               "formattedPhone":"(604) 596-7435"
            },
            "location":{
               "address":"7423 King George Hwy",
               "crossStreet":"btw 76 Avenue & 73A Avenue",
               "lat":49.138259617056015,
               "lng":-122.84723281860352,
               "distance":19000,
               "postalCode":"V3W 5A8",
               "city":"Surrey",
               "state":"BC",
               "country":"Canada",
               "cc":"CA"
            },
            "canonicalUrl":"https:\/\/foursquare.com\/v\/costco\/4abfb58ef964a520be9120e3",
            "categories":[
               {
                  "id":"4bf58dd8d48988d1f6941735",
                  "name":"Department Store",
                  "pluralName":"Department Stores",
                  "shortName":"Department Store",
                  "icon":{
                     "prefix":"https:\/\/foursquare.com\/img\/categories_v2\/shops\/departmentstore_",
                     "suffix":".png"
                  },
                  "primary":true
               }
            ],
            "verified":true,
            "restricted":true,
            "stats":{
               "checkinsCount":2038,
               "usersCount":533,
               "tipCount":12
            },
            "url":"http:\/\/www.costco.ca",
            "specials":{
               "count":0,
               "items":[

               ]
            },
            "hereNow":{
               "count":0,
               "groups":[

               ]
            },
            "referralId":"v-1366316196"
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

我做了这样一堂课

 public class Response …
Run Code Online (Sandbox Code Playgroud)

c# json foursquare restsharp

9
推荐指数
2
解决办法
2万
查看次数

如何在Https请求中添加RestSharp添加客户端证书?(C#)

如何在Https请求中添加RestSharp添加客户端证书?我的代码不起作用.

    public static IRestResponse<User> AsyncHttpRequestLogIn(string path, string method, object obj)
    {
        var client = new RestClient(Constants.BASE_URL + path); // https:....
        var request = method.Equals("POST") ? new RestRequest(Method.POST) : new RestRequest(Method.GET);
        request.RequestFormat = RestSharp.DataFormat.Json;

        // The path to the certificate.
        string certificate = "cer/cert.cer";     

        client.ClientCertificates.Add(new X509Certificate(certificate));

        request.AddBody(
            obj
        );


        IRestResponse<User> response = client.Execute<User>(request);

        return response;

    }
Run Code Online (Sandbox Code Playgroud)

c# ssl x509certificate restsharp

9
推荐指数
1
解决办法
2万
查看次数

使用RESTSHARP时,API返回错误

当使用RestSharp调用API时,我收到此错误:

底层连接已关闭:发送时发生意外错误.

我已经验证我的客户端ID,密码,用户名和密码是否正确.我能够在没有PowerShell问题的情况下做到这一点.

public string GetTokenForBrightIdea()
{
    RestClient restclient = new RestClient(_uri);
    RestRequest request = new RestRequest() { Method = Method.POST };

    request.AddHeader("Accept", "application/json");
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("grant_type", "password");
    request.AddParameter("client_id", _clientId);
    request.AddParameter("client_secret", _clientSecret);
    request.AddParameter("username", _clientUsername);
    request.AddParameter("password", _clientPassword);

    var tResponse = restclient.Execute(request);
    var responseJson = tResponse.Content;
    return JsonConvert.DeserializeObject<Dictionary<string, object>>(
        responseJson)["access_token"].ToString();
}
Run Code Online (Sandbox Code Playgroud)

使用RestSharp进行此操作时我错过了什么?

c# api rest restsharp

9
推荐指数
2
解决办法
3156
查看次数

RestSharp:无法加载文件或程序集“System.Text.Json,版本=7.0.0.0”

我在 Visual Studio 中有一个 C#(Azure 函数应用程序)项目。.Net 6.0

我将 RestSharp 更新到版本 110.2.0,现在出现此错误:

[2023-04-29T21:34:10.399Z] Executed 'RequestItemsPage' (Failed, Id=8eefbb7c-3bc0-44db-81e4-14c56096fc81, Duration=63ms)
[2023-04-29T21:34:10.401Z] System.Private.CoreLib: Exception while executing function: RequestItemsPage. System.Private.CoreLib: Exception has been thrown by the target of an invocation. RestSharp: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
[2023-04-29T21:34:10.408Z] eb4802876b9e44839f63422be68619dc: Function 'RequestItemsPage (Activity)' failed with an error. Reason: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
[2023-04-29T21:34:10.411Z]  ---> System.IO.FileNotFoundException: Could not load …
Run Code Online (Sandbox Code Playgroud)

.net c# restsharp azure-functions

9
推荐指数
2
解决办法
5555
查看次数

如何在OAuth中使用RestSharp?

我很困惑我应该使用哪个工厂OAuth1Authenticator工厂方法.我想我应该得到一个消费者秘密令牌(我可以用RestSharp获取吗?),然后使用OAuth1Authenticator.ForRequestToken,然后获取访问令牌和秘密访问令牌(如何?),然后使用OAuth1Authenticator.ForAccessToken并使用此返回值向前看.

但似乎RestSharp被设计为使用唯一的一个身份验证器,我似乎找不到从冷启动(只有app令牌)到拥有所有必要凭据(消费者密钥和秘密,访问密钥和秘密)的方法.

奖金问题:

  1. 什么.ForClientAuthentication和.ForProtectedResource方法适合?
  2. 为什么OAuth2Authenticatior与OAuth1Authenticator位于不同的名称空间?

oauth restsharp

8
推荐指数
1
解决办法
2万
查看次数