使用RestSharp时响应不正确,但是对于httpclient来说效果很好

时间:2018-09-13 15:24:02

标签: c# restsharp

如果我使用RestSharp,则会在我的Json上收到一个错误的请求,但是它与httpClient一起很好地工作。来自API的响应无法重新识别,并且请求不正确。在调试器中,第一个字符串SerialNumber为NULL。有人可以帮助您修复编码。

            RestClient client = new RestSharp.RestClient(BaseUrl);
            RestRequest request = new RestSharp.RestRequest("/api/mobileservice/commissionv2", RestSharp.Method.POST);
            request.RequestFormat = DataFormat.Json;
            request.AddHeader("Content-Type", "application/json; charset = utf - 8");
            var strJSONContent = new Dictionary<string, string>
            {
                {"NexoController.SerialNumber", "AJUTESTDEVICE" },
                {"NexoController.FirmwareVersion", "80" }, 
                {"Retailer.Name", "TestShop" },
                {"Retailer.StoreId", "1" },
                {"Retailer.HasCompetitorCoolers", "true" },
                {"Retailer.CompetitorCoolerCount", "D82" },
                {"Cooler.CoolerManufacturerId", "0"},
                {"Cooler.ModelId", "1"},
                {"Cooler.ShelfCount", "5"},
                {"Cooler.Serial", "string"},
                {"Cooler.PositionId", "0"},
                {"Cooler.CustomerAssetId", "AJUTESTCUSTOMERASSETID"},
                {"Cooler.IsLocatedNextToCompetitorCooler", "true"},
                {"Cooler.ChannelId", "0"},
                {"Cooler.CoolerOtherManufacturerName", "string"},
                {"Cooler.CoolerOtherModelName", "string"},
                {"Beacon.Major", "210" },
                {"Beacon.Minor", "240" },
                {"PeriodTimeUTC", "string"},
                {"UtcOffset", "0"},
                {"Meta.BrandId", "1"},
                {"Meta.AccountNameId", "1"},
                {"DevicePosition.SerialNumber", "string"},
                {"DevicePosition.DeviceId", deviceId},
                {"DevicePosition.FirmwareVersion", "string"},
                {"DevicePosition.MDeviceUUID", "1"},
                {"DevicePosition.Position.Lat", "1"},
                {"DevicePosition.Position.Long", "1"},
                {"DevicePosition.Accuracy", "1"},
                {"DevicePosition.RSSI", "1"},
                {"DevicePosition.DetectionTypeId", "string" },
                {"DevicePosition.LocationAgeSeconds","0" },
                {"DevicePosition.PeriodTimeUTC","string" },
                {"DevicePosition.UtcOffset","0" },
                {"DevicePosition.UploadedBy","Aju" }
            };                
            //add parameters and token to request
            request.Parameters.Clear();                
            request.AddParameter("application/json", JsonConvert.SerializeObject(strJSONContent), ParameterType.RequestBody);
            request.AddParameter("Authorization", "Bearer " + authToken, ParameterType.HttpHeader);
            //make the API request and get a response
            var response = client.Execute(request);

1 个答案:

答案 0 :(得分:0)

如果您创建POCO来存储JSON内容,则可以使用AddJsonBody方法, 身体是物体在哪里:

RestClient client = new RestSharp.RestClient(BaseUrl);
RestRequest request = new RestSharp.RestRequest("/api/mobileservice/commissionv2", RestSharp.Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json; charset = utf - 8");

request.AddJsonBody(body); 

request.AddParameter("Authorization", "Bearer " + authToken, ParameterType.HttpHeader);
var response = client.Execute(request);

并且RestSharp应该在内部处理序列化。 POCO的属性名称应与JSON的键匹配