0

I am calling an api and trying to post data to it but the data is always null. The api is expeting List<KeyValuePair<string,string>>. Here is the api call and the sample code from api.

API Call (Python)

import requests

data= {'CRHandle': 'Handle',
       'UpdateParam':[{'Key':'Value'},{'Key1':'Value'}]}

resp_out = requests.post("http://localhost:56011/api/values/TestData",data = data)
print(resp_out.status_code)
data_out = resp_out.text

API Method (.NET)

[HttpPost]
public string TestData([FromBody] TicketUpdateModel updateData)
{             
    return JsonConvert.SerializeObject(updateData);
}

Data Model

public class TicketUpdateModel
{
    public string CRHandle { get; set; }
    public List<KeyValuePair<string, string>> UpdateParam  { get; set; }
}

Data received at API end enter image description here

What am I doing wrong?

Rohit
  • 1,520
  • 2
  • 17
  • 36
  • 1
    I am guessing that sending `''UpdateParam':[{'Key':'MyKey1', 'Value': 'MyValue1'},{'Key':'MyKey2', 'Value': 'MyValue2'}]` should do. The reason being, `Key` and `Value` are properties of `KeyValuePair` as against a single property named `Key`. Does that work? – shahkalpesh Jun 20 '21 at 19:10
  • @shahkalpesh, it did not work, its just that the count increased form 2 to 4. – Rohit Jun 20 '21 at 19:17

1 Answers1

0

Here is a sample program I tried. See if this helps.

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

public class Program
{
  public static void Main()
  {
    var tm = new TicketUpdateModel() {
      CRHandle = "blah",
      UpdateParam = new List<KeyValuePair<string, string>>() {
        new KeyValuePair<string, string>("Hello1", "WOrld1"),
        new KeyValuePair<string, string>("Hello2", "WOrld2"),
      }
    };

    var json = JsonConvert.SerializeObject(tm);
    // prints {"CRHandle":"blah","UpdateParam":[{"Key":"Hello1","Value":"WOrld1"},{"Key":"Hello2","Value":"WOrld2"}]}
    Console.WriteLine(json);

    var tmd = JsonConvert.DeserializeObject<TicketUpdateModel>(json);
    Console.WriteLine(tmd.CRHandle);
    Console.WriteLine(tmd.UpdateParam[0].Key);
    Console.WriteLine(tmd.UpdateParam[1].Value);
    


    return;
  }
}


public class TicketUpdateModel
{
    public string CRHandle { get; set; }
    public List<KeyValuePair<string, string>> UpdateParam  { get; set; }
}
shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
  • I am facing problem with api call in python.. not sure how this is going to help – Rohit Jun 21 '21 at 04:28
  • 1
    The code example was to show, how the json has to be structured so a model object can be created from POST body. How about setting `content-type` header to `application/json` at python side ? – shahkalpesh Jun 21 '21 at 06:26
  • this was the problem, I was able to fix it. changed the `data` to `json` while sending the parameter. followed this post https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests?rq=1 – Rohit Jun 21 '21 at 06:29
  • Also, I had to use `Key` & `Value` keyword to pass actual Key and Value `{'Key':'Key1', 'Value': 'Value'}` – Rohit Jun 21 '21 at 06:36
  • @Rohit: Isn't that what I wrote re. the json in comments as well my reply (to show you how the json needs to be sent for it to create an instance) ? – shahkalpesh Jun 21 '21 at 06:37
  • yes, i figured that out. it needed combination of both. thanks (y) – Rohit Jun 21 '21 at 06:41