-1

I'm having an issue where I am trying to post a class I have with several properties and there are properties that are of type List that are for some reason not POSTing on the server.

This is an example JSON of an item from the webserver

{
 "at": "AA",
 "campaign": "Unknown",
 "createdby": {
  "userid": "user001",
  "writetime": "2021-06-13T23:43:21.600446Z"
 },
 "geographies": [
  {
   "country": "USA",
   "province": "New York",
   "city": "Albany",
   "createdby": {
    "userid": "user001",
    "writetime": "2020-06-17T15:46:45.214185Z"
   },
   "id": "db3faf37-88d3-46fd-8ca5-54898c3450fc",
   "source": "Manual Entry",
   "userid": ""user001",",
   "writetime": "2020-06-17T15:46:45.214185Z"
  }
 ],
 "isVerified": true,
 "isValid": false,
 "platforms": [],
 "remarks": {},
 "roe": "001",
 "dtg": "2021-06-13T16:42:00Z",
 "identifier": "10101",
 "type": "DELIBERATE",
 "id": "cd4e2c93-4215-466c-b680-044a094477b5",
 "rowid": "e70e9253-1946-4497-ba8e-58216c1a1e39",
 "source": "Manual Entry",
 "userid": "user001",
 "writetime": "2021-06-13T23:43:21.600446Z",
 "readgroups": [
  "public"
 ],
 "writegroups": [
  "public"
 ],
 "classification": {
  "level": "0",
  "aea": {
   "value": ""
  },
  "classificationString": "",
  "portionString": ""
 }
}

When I create my own item using a C# class, I serialize it and this is what it looks like

{
  "at": "FE",
  "campaign": "Unknown",
  "createdby": {
    "userid": "user001",
    "writetime": "2021-06-27T12:00:00"
  },
  "geographies": [
    {
      "country": "USA",
      "province": "Florida",
      "city": "Miami",
      "createdby": {
        "userid": "user001",
        "writetime": "2021-06-27T12:00:00"
      },
      "id": "db3faf37-88d3-46fd-8ca5-54898c3450fc",
      "source": "Manual Entry",
      "userid": "user001",
      "writetime": "0001-01-01T00:00:00"
    }
  ],
  "isVerified": true,
  "isValid": false,
  "munitions": [],
  "operation": null,
  "platforms": [],
  "remarks": {
    "remark1": null,
    "remark2": null
  },
  "roe": "009",
  "dtg": "2021-06-27T12:00:00",
  "identifier": "77896",
  "type": "DYNAMIC",
  "unit": null,
  "id": "cd4e2c93-4215-466c-b680-044a094477b5",
  "rowid": "e70e9253-1946-4497-ba8e-58216c1a1e39",
  "source": null,
  "userid": "user001",
  "writetime": "0001-01-01T00:00:00",
  "readgroups": [
    "public"
  ],
  "writegroups": [
    "public"
  ],
  "classification": null
}

And the result from the post is successful, and I see it on the webserver but the geography is not being posted for some reason.

This is my C# code if that helps

MyItemClass item;           

item = new MyItemClass
{
    id = _id,
    rowid = _rowid,
    at = _at,
    campaign = _campaign,
    isVerified = _verified,
    isValid = _valid,
    operation = _operation,
    roe = _roe,
    dtg = _dtg,
    identifier = _identifier,
    type = _type,
    unit = _unit,
    userid = _userId,
    createdby = new CreatedByClass()
    {
        userid = _userId,
        writetime = _writetime
    },
    remarks = new RemarksClass()
    {
        remark1 = _rem1,
        remark2 = _rem2
    },
    readgroups = new List<string>() { "public" },
    writegroups = new List<string>() { "public" },

    geographies = new List<Geography>(),
    platforms = new List<Platform>(),
};

// import Location data
foreach (LocationsModel location in ListOfLocations)
{
    if (location.SelectedCity == null)
        location.SelectedCity = string.Empty;
    if (location.SelectedProvince == null)
        location.SelectedProvince = string.Empty;

    item.geographies.Add(new Geography()
    {
        city = location.SelectedCity,
        country = location.SelectedCountry,
        province = location.SelectedProvince,
        id = location.LocationGuid.ToString(),
        userid = _userid,
        source = _source,
        createdby = new CreatedByClass()
    });
}

string ItemJsonString = JsonConvert.SerializeObject(item);
JObject data = JObject.Parse(ItemJsonString);

string newJson = data.ToString();

var Content = new StringContent(newJson, Encoding.UTF8, "application/json");
var result = httpClient?.PostAsync($"<URL>", Content).Result;
            

From sources online, there are few ways I saw how to perform a POST and this is one of them, but from what I have tried, none of them have worked and I'm not sure why.

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
redvolt12
  • 21
  • 3
  • @Austin if you are asking prior to serializing it, where I hold it into the newJson variable, I have outputted the string to a text file to verify it is in there and it is. – redvolt12 Jun 14 '21 at 15:22

1 Answers1

0

try this. It was tested in Visual Studio and working properly

data

    var jsonString = "{\"at\":\"AA\",\"campaign\":\"Unknown\",\"createdby\":{\"userid\":\"user001\",\"writetime\":\"2021-06-13T23:43:21.600446Z\"},\"geographies\":[{\"country\":\"USA\",\"province\":\"New York\",\"city\":\"Albany\",\"createdby\":{\"userid\":\"user001\",\"writetime\":\"2020-06-17T15:46:45.214185Z\"},\"id\":\"db3faf37-88d3-46fd-8ca5-54898c3450fc\",\"source\":\"Manual Entry\",\"userid\":\"user001\",\"writetime\":\"2020-06-17T15:46:45.214185Z\"}],\"isVerified\":true,\"isValid\":false,\"platforms\":[],\"remarks\":{},\"roe\":\"001\",\"dtg\":\"2021-06-13T16:42:00Z\",\"identifier\":\"10101\",\"type\":\"DELIBERATE\",\"id\":\"cd4e2c93-4215-466c-b680-044a094477b5\",\"rowid\":\"e70e9253-1946-4497-ba8e-58216c1a1e39\",\"source\":\"Manual Entry\",\"userid\":\"user001\",\"writetime\":\"2021-06-13T23:43:21.600446Z\",\"readgroups\":[\"public\"],\"writegroups\":[\"public\"],\"classification\":{\"level\":\"0\",\"aea\":{\"value\":\"\"},\"classificationString\":\"\",\"portionString\":\"\"}}";
    

code

 var Content = new StringContent(jsonString , Encoding.UTF8, "application/json");
var result = httpClient?.PostAsync($"<URL>", Content).Result;

this was tested too

Code


var jsonObj = JsonConvert.DeserializeObject<Data>(jsonString);
var json = JsonConvert.SerializeObject(jsonObj);
    
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, content);
    
    if (response.IsSuccessStatusCode)
    {
        var stringData = await response.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<Data>(stringData);
    }

classes


public class Createdby
{
    public string userid { get; set; }
    public DateTime writetime { get; set; }
}

public class Geography
{
    public string country { get; set; }
    public string province { get; set; }
    public string city { get; set; }
    public Createdby createdby { get; set; }
    public string id { get; set; }
    public string source { get; set; }
    public string userid { get; set; }
    public DateTime writetime { get; set; }
}

public class Remarks
{
    public string remark1 { get; set; }
    public string remark2 { get; set; }
}

public class Aea
{
    public string value { get; set; }
}

public class Classification
{
    public string level { get; set; }
    public Aea aea { get; set; }
    public string classificationString { get; set; }
    public string portionString { get; set; }
}

public class Data
{
    public string at { get; set; }
    public string campaign { get; set; }
    public Createdby createdby { get; set; }
    public List<Geography> geographies { get; set; }
    public bool isVerified { get; set; }
    public bool isValid { get; set; }
    public List<object> platforms { get; set; }
    public Remarks remarks { get; set; }
    public string roe { get; set; }
    public DateTime dtg { get; set; }
    public string identifier { get; set; }
    public string type { get; set; }
    public string id { get; set; }
    public string rowid { get; set; }
    public string source { get; set; }
    public string userid { get; set; }
    public DateTime writetime { get; set; }
    public List<string> readgroups { get; set; }
    public List<string> writegroups { get; set; }
    public Classification classification { get; set; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • It doesn't work. I have used this method before. – redvolt12 Jun 14 '21 at 15:28
  • I saw your edited code, I have explicitly used your values and for some reason it does not post the geography section. I don't know why. Granted I don't have control over the web server so I am in contacts with them to see if they can assist me with it. – redvolt12 Jun 14 '21 at 16:37
  • @redvolt12 Yes, if it still doesn't go it means your API has problems. PLs post API code too. – Serge Jun 14 '21 at 18:26