使用相同的uri参数传递,得到两个不同的结果

时间:2018-11-09 15:26:24

标签: c# json rest

我正在尝试从API获取100张帮助台票证的列表。网址在下面。

https://sdpondemand.manageengine.com/app/itdesk/api/v3/requests?input_data={"list_Info":{"row_count":100,"start_index":101}}

我能够产生成功的结果,这意味着当我将uri放入这样的字符串时,它将带回从索引101开始的100行:

string extra = "app/itdesk/api/v3/requests?input_data={\"list_info\":{\"row_count\":100,\"start_index\":101}}";

但是如果我尝试将json放入类中,然后使用Json.Net库对其进行序列化,它将失败,这意味着它仅返回索引1上的10行。

private class input_data
{
    public list_info list_Info = new list_info();
}
private class list_info
{
    public int row_count = 100;
    public int start_index = 101;
}

input_data input = new input_data();
string json = Newtonsoft.Json.JsonConvert.SerializeObject(input);
string extra1 ="app/itdesk/api/v3/requests?input_data="+json;

我看到两个请求完全相同。我在做什么错了?

代码中的var外观

extra: app/itdesk/api/v3/requests?input_data={"list_info":{"row_count":100,"start_index":101}}
extra1: app/itdesk/api/v3/requests?input_data={"list_Info":{"row_count":100,"start_index":101}}

2 个答案:

答案 0 :(得分:1)

在Get请求中传递序列化的DTO对象不是实现API的正确方法。获取应该具有参数而不是序列化对象的请求。如果您希望这样做并且必须发送对象,那么为什么不使用发布请求。

rest api的示例实现可以是:

通过GET

[Route("{rowCount}/{startIndex}"), HttpGet]
public IHttpActionResult Get(int rowCount, int startIndex)
{
     //Your logic Implementation
}

呼叫就像

www.xyz.com/controllerName/100/101

这是请求的其余实现

通过POST

[Route(""), HttpPost]
public IHttpActionResult Post([FromBody]YourDTOClass obj)
{
     //Your logic Implementation
}

例如,您拥有DTO类

//In C# the class name should be capital
private class ListInfo
{
    //In c# the property name should be Capital
    public int RowCount {get; set;} = 100;    
    public int StartIndex {get; set;}= 101;
}

所以您的Post方法看起来像

//Route attribute is for configuring the custom route 
//It is a feature in MVC 5
//FromBody attribute will search for data in the request body
[Route(""), HttpPost]
public IHttpActionResult Post([FromBody]ListInfo info)
{
     //Your logic Implementation
}

如果您也使用C#调用API,则可以在将类的json对象传递数据的地方使用HttpClient

已编辑:由于您使用的是第三方API,因此需要更正调用。

using (var client = new HttpClient())
{
     //Setting the base address of the server 
    client.BaseAddress = new Uri("https://sdpondemand.manageengine.com");

    //creating an anonymous object
    var jsonObject = new {
        input_data = new {
             row_count = 100,
             start_index = 101
        }
    };

    //Converting into the content string
    var content = new StringContent(JsonConvert.SerializeObject(jsonObject), Encoding.UTF8, "application/json");

    //waiting for the post request to complete
    var result = await client.PostAsync("app/itdesk/api/v3/requests", content);

    //reading the response string 
    string resultContent = await result.Content.ReadAsStringAsync();
    if (response.IsSuccessStatusCode)
    {
        //Deserialize your string into custom object here
        var obj = JsonConvert.DeserializeObject<YourDTO>(resultContent);
    }
    else
    {
        //Todo: Log the Exception here
        throw new Exception(contentString);
    }
}

答案 1 :(得分:0)

  

额外:app / itdesk / api / v3 / requests?input_data = {“ list_info” {“ row_count”:100,“ start_index”:101}}   extra1:app / itdesk / api / v3 / requests?input_data = {“ list_Info”:{“ row_count”:100,“ start_index”:101}}

弄清楚列表信息

  

额外:app / itdesk / api / v3 / requests?input_data = {“ list_info” {“ row_count”:100,“ start_index”:101}}   extra1:app / itdesk / api / v3 / requests?input_data = {“ list_info”:{“ row_count”:100,“ start_index”:101}}

[耸肩表情符号]