Sending json through api post request

Edd27 1 Reputation point
2022-04-30T08:52:26.717+00:00

I'm currently struggling to send a JSON via post request into my controller to use it further by calling a stored procedure.

Here is the post request using axios:

async returnJSON(model) {
      this.info = await axios.post(
          "https://localhost:44349/api/items/AddItem/",model)
        .then((response) => response.data);

Here is the structure of model

{
  "arrayAutori": [
    {
      "dinUniversitate": true,
      "creatorType": 4,
      "creatorID": 3
    }
  ],
  "itemID": "",
  "itemTypeID": 6,      
  "drepturiDeAutor": {
    "valueID": "",
    "fieldID": 15
  },
  "isbn": {
    "valueID": "",
    "fieldID": 25
  },
  "limba": {
    "valueID": "",
    "fieldID": 7
  },
  "numarPagini": {
    "valueID": "",
    "fieldID": 43
  },
  "data": {
    "valueID": "",
    "fieldID": 6
  },
  "editura": {
    "valueID": "",
    "fieldID": 23
  },
  "editie": {
    "valueID": "",
    "fieldID": 42
  },
  "volum": {
    "valueID": "",
    "fieldID": 19
  },
  "numarColectie": {
    "valueID": "",
    "fieldID": 41
  },
  "titlu": {
    "valueID": "",
    "fieldID": 1
  }
}

And here is my controller:

public string AddItem( JObject jsonString)
       {
           db.Database.ExecuteSqlCommand("exec zotero.PublicationsMerge " + jsonString);
           db.SaveChanges();
           return "1";
       }

I can not make a model class to use it, because that JSON may vary depending on the publications. Also, when I'm debugging this controller, jsonString looks like this:

{{
  "arrayAutori": [
    {
      "dinUniversitate": true,
      "creatorType": 4,
      "creatorID": 3
    }
  ],
  "itemID": "",
  "itemTypeID": 6,
  "drepturiDeAutor": {
    "valueID": "",
    "fieldID": 15
  },
  "isbn": {
    "valueID": "",
    "fieldID": 25
  },
  "limba": {
    "valueID": "",
    "fieldID": 7
  },
  "numarPagini": {
    "valueID": "",
    "fieldID": 43
  },
  "data": {
    "valueID": "",
    "fieldID": 6
  },
  "editura": {
    "valueID": "",
    "fieldID": 23
  },
  "editie": {
    "valueID": "",
    "fieldID": 42
  },
  "volum": {
    "valueID": "",
    "fieldID": 19
  },
  "numarColectie": {
    "valueID": "",
    "fieldID": 41
  },
  "titlu": {
    "valueID": "",
    "fieldID": 1
  }
}}

I don't know why, but it adds another pair of {}. What am I doing wrong?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jaliya Udagedara 2,731 Reputation points MVP
    2022-05-01T13:56:41.793+00:00

    As you have noted, the JSON you are passing is not correct (additional curly braces). Once you fix that, JObject will get populated. Right now it should be null?

    0 comments No comments

  2. Bruce (SqlWork.com) 56,026 Reputation points
    2022-05-01T16:54:10.223+00:00

    Even if the json string is valid, the following

    public string AddItem( JObject jsonString)
    {
    db.Database.ExecuteSqlCommand("exec zotero.PublicationsMerge " + jsonString);
    db.SaveChanges();
    return "1";
    }

    Won’t work because it’s not valid sql. Sql does not support json literals. Maybe the sp is expecting a string variable, that is parsed. Pass the json value a string parameter .

    0 comments No comments