2017-02-23 2 views
0

RestSharp의 게시물 요청에 문제가 있습니다. 나는이 개 수업이 있습니다RestSharp - 게시 요청 문제 (작업 실패)

public class UnitToPost 
     { 
      public bool floating_point { get; set; } 
      public Dictionary<string, TranslationUnitToPost> translations { get; set; } 
     } 

     public class TranslationUnitToPost 
     { 
      public string name { get; set; } 
     } 

을 그리고 난 후 요청을 보낼 :

 client = new RestClient(adresApi); 

     client.AddDefaultHeader("Authorization", "Bearer " + key);   

     IRestRequest updateProduct = new RestRequest("units", Method.POST); 

     ShoperModel.UnitToPost unitToPost = new ShoperModel.UnitToPost(); 
     unitToPost.floating_point = true; 
     ShoperModel.TranslationUnitToPost transUnit = new ShoperModel.TranslationUnitToPost(); 
     transUnit.name = "namename"; 
     unitToPost.translations = new Dictionary<string, ShoperModel.TranslationUnitToPost>(); 
     unitToPost.translations.Add("pl_PL", transUnit); 


     updateProduct.RequestFormat = RestSharp.DataFormat.Json; 
     updateProduct.AddBody(unitToPost); 

     IRestResponse updateProductResponse = this.client.Execute(updateProduct); 

그리고 난 항상 오류 얻을 :

[RestSharp.RestResponse] = "StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)"

Content = "{\"error\":\"server_error\",\"error_description\":\"Operation Failed\"}"

그것의 원인은 무엇입니까? 그것은 내 수업에서 사전 때문일 수 있습니까?

답변

0

코드를 실행했으며 유효한 JSON 본문으로 요청을 보냅니다.

POST http://..../units HTTP/1.1

Accept: application/json,application/xml, text/json, text/x-json, text/javascript, text/xml Authorization: Bearer a

User-Agent: RestSharp/105.2.3.0

Content-Type: application/json

Host: .....

Content-Length: 84

Accept-Encoding: gzip, deflate

Connection: Keep-Alive

{"floating_point":true,"translations":[{"Key":"pl_PL","Value":{"name":"namename"}}]}

수신 서버에 문제가있는 것 같습니다. 아직 그렇게하지 않으면 Fiddler (http://www.telerik.com/fiddler)를 실행하고 요청/응답을 검사하는 것이 좋습니다.

편집 ...

난 단지 당신이 JSON 몸이 원하는 실현

: - { "floating_point"사실, "번역": { "pl_PL": { "이름": "namename" https://github.com/restsharp/RestSharp/issues/696

이 누군가가 필요한 결과를 얻기 위해 ExpandoObject을 사용한 게시물을 포함 -은}}}

난이 커버하는 RestSharp 문제를 발견했다.

updateProduct.AddBody(JsonConvert.SerializeObject(unitToPost)); 
- :

http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/

그러나, 나는 쉽게에는 직렬화하고 다음 코드로 몸을 설정하는 JSON .NET을 사용하여 발견