Home > Back-end >  JSON Parameter is null on calling REST API
JSON Parameter is null on calling REST API

Time:12-08

I'm trying to send a URL as content from my client to my web api.
I don't want to encode it, so I'm sending it in the body as JSON.

Sending the JSON as

"{"URL":"https://www.example.com/s/otherdetail"}"

If I use method signature

[HttpPost("UploadURL/{SpecID}/{DocType}")]
public ActionResult<string> UploadSpecsURL(int SpecID, string DocType, [FromBody] JsonElement body) {
    string json = System.Text.Json.JsonSerializer.Serialize(body);

then I get content

body = ValueKind = Object : "{"URL":"https://www.example.com/s/otherdetail"}"
json = "{\"URL\":\"https://www.example.com/s/otherdetail\"}"

but if I try to define my own type to receive the content

public struct URLpacket {
    public string URL;
}

[HttpPost("UploadURL/{SpecID}/{DocType}")]
public ActionResult<string> UploadSpecsURL(int SpecID, string DocType, [FromBody] URLpacket packet) {

I don't get an error, but the URL is null.

CodePudding user response:

you have to add getters setters to your class

public struct URLpacket {
    public string URL { get; set; }
}
  • Related