Read curl header content webapi (easy) RRS feed

  • Question

  • I have the very simplest webApi (rest ) as below - the default build of the web API. 

    I added Swagger for a UX test environ. - and you can see images and see the code to inspect input

    public void Post([FromBody]string value)
                HttpContent requestContent = Request.Content;
                System.Net.Http.Headers.HttpHeaders  hd = Request.Headers;
                string stringFromBody = requestContent.ReadAsStringAsync().Result;
                Console.WriteLine("Where is it");

    a breakpoint has value as null and the variable stringFromBody is null

    The curl looks like this : curl -X POST --header 'Content-Type: application/json' -d 'test' 'http://localhost:57653/api/Values'

    A watch set on content headers shows the length, but cannot drill into it to locate value....

    How do I read for  the value test ?? ?   I have looked and looked on the internet..


    Monday, September 17, 2018 6:10 PM

All replies

  • I believe the WebHost controller tries to interpret the body.  You told it you are sending application/json data, but the string 'test' is not a JSON object.  If you want to send plain text, specify text/plain, not application/json.  Otherwise, you can try sending actual JSON, which means sending double-quotes:

        -d """json"""

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, September 17, 2018 9:45 PM
  • Tim appreciate your time to weigh in 

    from the swagger interface I can select 5  types of parameter content type 1.) Application/X-www-form-urlencoded, 2) application/Json (default) 3. text /JSON 4 application/XML. text/XML  

    In every case the Post ([FromBody] string value) is not found at breakpoint. The watch shows the content length correctly for the HttpContent.

    in the default template solution the Post is decorated with  Post([FromBody]string ). I can see the input from swagger and see it goes in but I cannot find how to extract it... How does one get the value  Post([FromBody] string value ) ??  Any idea on that [FromBody] mystery appreciated.

    Thanks !!!


    Tuesday, September 18, 2018 1:19 AM