question

Edd27-0692 avatar image
0 Votes"
Edd27-0692 asked JackJJun-MSFT commented

Sending json through api post request

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?




dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You want to pass a JSON formatted string and save the string? If so, then simply configure the axios client to send text/html content type rather than application/json. Update the action parameter to a string.

0 Votes 0 ·

Unfortunately, it doesn't work...

0 Votes 0 ·

@Edd27-0692 , Welcome to Microsoft Q&A, I noted that you are using controller. Then, could you tell me what is your current project type, is asp.net mvc or asp.net core mvc?

0 Votes 0 ·
JaliyaUdagedara avatar image
0 Votes"
JaliyaUdagedara answered

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?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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 .

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.