【问题标题】:restsharp PUT request error --> "StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)"restsharp PUT 请求错误 --> “StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)”
【发布时间】:2020-02-17 13:53:32
【问题描述】:

我想通过 REST API 发出 PUT 请求。我有access_token,应该是正确的。我正在尝试上传 JSON 关系对象,但出现以下错误:

restsharp PUT 请求错误:

“状态码:InternalServerError,内容类型:应用程序/json, 内容长度:-1)”

public void Store(HiveClient hiveClient,string _putUrl, string _url, string _clientId, string _clientSecret, HiveObject objToStore)
{
    hiveClient = new HiveClient(_url, _clientId, _clientSecret);
    hiveClient.CheckIfFileExists();
    string token = hiveClient.restToken;
    HiveObject hiveObject = objToStore;
    var json = JsonConvert.SerializeObject(hiveObject);
    RestClient restClient = new RestClient(_putUrl);
    RestRequest request = new RestRequest(Method.PUT);
    request.AddHeader("Authorization", token);
    request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
    request.AddJsonBody(json);
    request.RequestFormat = DataFormat.Json;
    IRestResponse respons = restClient.Execute<HiveObject>(request);
    var deserialize = new JsonDeserializer();
    var output = deserialize.Deserialize<Dictionary<string, string>>(respons);
} 

【问题讨论】:

  • 问题是?
  • 如果需要授权怎么办?因为我认为令牌总是需要基本或不记名作为授权标头。

标签: c# request restsharp put


【解决方案1】:

我建议阅读文档,真的。

public void Store(string _putUrl, string _url, string _clientId, string _clientSecret, HiveObject objToStore)
{
    var hiveClient = new HiveClient(_url, _clientId, _clientSecret);
    hiveClient.CheckIfFileExists();
    string token = hiveClient.restToken;

    var restClient = new RestClient(_putUrl);
    var request = new RestRequest()
        .AddHeader("Authorization", token)
        .AddJsonBody(objToStore);

    var result = restClient.Put<Dictionary<string, string>>(request);
} 

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多