0

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

2 Answers 2

1

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");
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.