Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • James 23 posts 156 karma points
    Aug 16, 2022 @ 03:26
    James
    0

    Cant post parameter to API Controller

    I am trying to post an object to an API Endpoint.

    The parameter is not coming through. I have tried to use the [FromBody] parameter but it does not make any difference. I know my code works as I have used it in previous versions of umbraco.

    It works if i use GET but it posts in the query string and i'm sending alot of data so this is not an option.

    Update: I changed the type to Dynamic, and am receiving the object wrapped in two {{ tags. I try to use JsonConvert.SerializeObject to convert it to its type but get an exception "BasicAuthenticationMiddleware.cs not found"

    Any help would be much appreciated.enter image description here enter image description here

  • Johan Runsten 38 posts 277 karma points c-trib
    Aug 16, 2022 @ 06:00
    Johan Runsten
    0

    Hi!

    Try wrapping your parameter in an object, e.g FileDetailRequest or similar and taking that as a parameter instead.

  • Alexander Tkacuk 19 posts 120 karma points
    Aug 18, 2022 @ 22:48
    Alexander Tkacuk
    0

    when you read data from body in your controller, you need object like

    public class TestModel{
        public string Test {get; set;}
    }
    

    Then model builder from asp.net can map you request. You neet only cange method in your controller like this.

    public JsonResult Post([FromBody] TestModel testModel)
    
  • James 23 posts 156 karma points
    Aug 18, 2022 @ 22:58
    James
    0

    The issue was how i was stringifying the object. i used JSON.stringify({ test: "blah" }) but should of just been JSON.stringify({ "blah" }).

    Makes sense but was working the way i first done it on older versions of umbraco/.net

Please Sign in or register to post replies

Write your reply to:

Draft