Reputation: 115
My Json [Body] structure in postman is given below
[{
"SerialNumber": "dev1",
"CustomerId": "cust1",
"Seed": "c393d900-2f25-819f-cc83-0721357bcf10",
"BitLockerKey": null,
"bitLockerRecoveryKey": null
},
{
"SerialNumber": "dev2",
"CustomerId": "cust2",
"Seed": "c393d900-2f25-819f-cc83-0721357bcf22",
"BitLockerKey": null,
"bitLockerRecoveryKey": null
}]
My web api function is given below,
[HttpPost]
public ActionResult<string> SaveData([FromBody] string Data)
{
int result = _seedService.SeedGeneration(Data);
return result > 0 ? Ok(result) : (ActionResult<string>)UnprocessableEntity("Seed generation
Failed");
}
My question is, how to pass data as string with json array structure in postman. i got the error "JSON value could not be converted to System.string
Upvotes: 0
Views: 1173
Reputation: 115
I have installed Microsoft.AspNetCore.Mvc.NewtonsoftJson package & modified the function. removed string as parameter & included JsonElement in parameter.
[HttpPost]
public ActionResult<string> SaveData([FromBody] JsonElement Data)
{
string json = System.Text.Json.JsonSerializer.Serialize(Data);
int result = _seedService.SeedGeneration(json);
return result > 0 ? Ok(result) : (ActionResult<string>)UnprocessableEntity("Seed
generation Failed");
}
Upvotes: 1
Reputation: 96
Using JSON.stringify(jsonValue) you can convert a JSON object to string via javascript.
The result is the following:
"[{"SerialNumber":"dev1","CustomerId":"cust1","Seed":"c393d900-2f25-819f-cc83-0721357bcf10","BitLockerKey":null,"bitLockerRecoveryKey":null},{"SerialNumber":"dev2","CustomerId":"cust2","Seed":"c393d900-2f25-819f-cc83-0721357bcf22","BitLockerKey":null,"bitLockerRecoveryKey":null}]"
You can also declare the action parameter as an array of an object with the structure of your Json and the Asp.Net mechanism you handle it for you.
Upvotes: 0