D. Stewart D. Stewart - 1 year ago 176
C# Question

C# getting HTTP body using Nancy

quick question. I'm using Nancy to convert REST calls to api calls in a project I'm working on. I can't quite get it to work.

I've got Nancy setup and working, for GET requests it's fine. However when I make a POST request using the built in RestClient, like:

restClient.Post("/test", "Param");

I can't figure out how to get "Param" out once the call has been made.

I have the NancyModule setup as such:

public class TestNancyModule : NancyModule {
public TestNancyModule() {
Post["/test"] = p => {
for(KeyValuePair<dynamic, dynamic> kvp in (DynamicDictionary)p)
Console.WriteLine("{0}:{1}", kvp.Key, kvp.Value);

I have a breakpoint setup inside the NancyModule, which is getting hit when I make a post request to localhost/test, but I can't for the life of me figure out how to extract "Param" out once I'm inside the Nancy module. Ideally I'd be able to just do something like

string POSTParameters = p["Value"]
//POSTParameters now equals "Param"

Any suggestions?


Edit: I'm leaving the original post up, but I'll clarify here. I was meaning to ask how to access the HTTP body of incoming requests. Unfamiliarity with the protocol led to some errors on my part using the wrong terminology.

Answer Source

You can use .AsString() on the body if you pull in Nancy.Extensions

var thing = Request.Body.AsString();

This is the Nancy way.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download