Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Retreive JSON Data from HttpRequestMessage

Retreive JSON Data from HttpRequestMessage

Scheduled Pinned Locked Moved ASP.NET
securityjsonquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnalogNerd
    wrote on last edited by
    #1

    I'm doing a post to a WebAPI that passes a JSON object. Here's how the Post is set up:

    var jsonContent = new JavaScriptSerializer().Serialize(myObject);
    var request = (HttpWebRequest)WebRequest.Create(apiUrl);
    request.Date = dateOffset.DateTime;
    request.Method = method;
    using (var streamWriter = new StreamWriter request.GetRequestStream()))
    {
    streamWriter.Write(jsonContent)
    streamWriter.Flush();
    streamWriter.Close();
    }
    }

    In my WebAPI I have an attribute on my controller that inherits from ActionFilterAttribute. This serves to do some authentication and security checks before the controller is called. Ideally I'd like to retrieve the JSON string from the Request, but I can't figure out how. I tried this, but it didn't work:

    var contentTask = actionContext.Request.Content.ReadAsStringAsync();
    contentTask.Wait();
    var jsonContent = contentTask.Result;

    However, the content is always empty. What am I doing wrong? EDIT: For what it is worth, if I comment out this problematic code and let the call go on to my controller, the object is passed in and deserialized just fine.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups