How do I write a post method that accepts JSON in Nancy, and how do I call it from a C# client?


Tapo

I wrote the following module in Nancy

    public class CategoryModule : NancyModule
{
    public CategoryModule()
    {
        //At this moment just Show Hello world
        Get["/"] = _ => { return "Nancy says hello!"; };
        //Get["/"] = parameters => "Hello World!";
        GetCategories();
        SetCategory();
    }


     void GetCategories()
    {
        Get["/Catergories"] = _ =>
        {

            var catergoryRepository = new CategoryRepository();
            var categorycollection = catergoryRepository.GetCategoryInfo();
            return Negotiate.WithStatusCode(HttpStatusCode.OK).WithModel(categorycollection.ToArray());
        };
    }

     void SetCategory()
     {
         Post["/Catergories/{categryName:string}"] = _ =>
         {
             var catergoryModel = this.Bind<Category>();
             catergoryModel.PK_CategoryId = Guid.NewGuid();
             catergoryModel.CategoryName = _;
             return HttpStatusCode.OK;
         };
     }
}

I am using chrome POSTMAN to test the module. If I call http://192.168.1.4:8888/Categories in POSTMAN, I can get breakpoints in "GetCategories()" . But if I call http://192.168.1.4:8888/Catergories/categryName=test, I ca n't get the breakpoint in SetCategory() . I'm new to Nancy and not sure if my method of posting is correct.

anyone can provide

  1. Example of Post method accepting Jason as parameter
  2. Example of calling it from client

I couldn't find the simple example above in their documentation.

Notice

I'm hosting Nancy's self-hosted environment with the following code

var server = new Nancy.Hosting.Self.NancyHost(new Uri("http://192.168.1.4:8888"));

Below is the category model

    public class Category
{
    public Guid PK_CategoryId { get; set; }
    public string CategoryName { get; set; }

}
Roman Dubicin

If you want to use POSTMAN to post JSON, you should add the JSON-Content-Type header as described in the documentation (third paragraph). Set up POSTMAN:

  1. Set the host to, http://192.168.1.4:8888/Categoriesthen select POST.
  2. Add headers with Content-Typeand application/jsonas header and value respectively.
  3. Set rawand set JSONto type.
  4. Put it in { "CategoryName": "something" }the text field below.
  5. Click "Send".

Related


Nancy (C#): How do I get my post data?

Westerlund I am using Corona SDK to post data to my C# server: headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "color=red&size=small" local params = {} params.headers = headers params.body = bod

Nancy (C#): How do I get my post data?

Westerlund I am using Corona SDK to post data to my C# server: headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "color=red&size=small" local params = {} params.headers = headers params.body = bod

Nancy (C#): How do I get my post data?

Westerlund I am using Corona SDK to post data to my C# server: headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "color=red&size=small" local params = {} params.headers = headers params.body = bod

How do I write an API action method that accepts a file?

User 12310517 I'm trying to write an API endpoint that allows users to send files to my server using Asp.NET Core 3.1. I have the following action method that needs to respond to the client's request and handle the file validation and store the file. [HttpPost

How do I write a promise on the client side

Malik Awan | I don't know I have searched a lot and when I apply the code, the console pops up a lot of undefined errors I want to write a simple promise $(document).ready(function(event){ $('.hover-effect').on('click',function(event){

How do I actually write/end an async method in C#?

Meeheecaan I know I need to await something asyncin the tag method to make it async, but I don't get how to actually end the hole it creates. static async void Example() { int t = await getIntAsync(); WriteLine($"computer: {t}"); } Since getIntAsync i

Vue - how do I call another method from a callback?

Jay K23 get_data()I need to call the function once the function has save_key()finished executing, the problem is that I get Cannot read property 'get_data' of undefinedan error. I'm assuming I get it because the function is called from a callback. How can I fi

How do I call a method if the window size changes in C#

bv_Martn While I know something about event handlers, I don't know enough about how to implement them. So the question is: how can I call my method redrawWidgets(); If the main window changes size without having to manually check each time? Note that I didn't

How do I call a method if the window size changes in C#

bv_Martn While I know something about event handlers, I don't know enough about how to implement them. So the question is: how can I call my method redrawWidgets(); If the main window changes size without having to manually check each time? Note that I didn't

In C#, how do I call a method on a possibly null instance?

el baid I'm wondering, using the Assume method IsNullOrEmpty()on an Listinstance (not in the .NET Framework), if the instance is actually null , it will throw an Object reference to the instance not set to Object exception , or something like that. With that i

In C#, how do I call a method on a possibly null instance?

el baid I'm wondering, using the Assume method IsNullOrEmpty()on an Listinstance (not in the .NET Framework), if the instance is actually null , it will throw an Object reference to the instance not set to Object exception , or something like that. With that i

How do I make the template call a method?

Alan 2 I have this template: <?xml version="1.0" encoding="utf-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Japanese.OpenPageTemplate" x:Name="this"

How do I make the template call a method?

Alan 2 I have this template: <?xml version="1.0" encoding="utf-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Japanese.OpenPageTemplate" x:Name="this"

How do I make the template call a method?

Alan 2 I have this template: <?xml version="1.0" encoding="utf-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Japanese.OpenPageTemplate" x:Name="this"

How do I make the template call a method?

Alan 2 I have this template: <?xml version="1.0" encoding="utf-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Japanese.OpenPageTemplate" x:Name="this"

How do I make the template call a method?

Alan 2 I have this template: <?xml version="1.0" encoding="utf-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Japanese.OpenPageTemplate" x:Name="this"

How do I make the template call a method?

Alan 2 I have this template: <?xml version="1.0" encoding="utf-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Japanese.OpenPageTemplate" x:Name="this"

How do I make the template call a method?

Alan 2 I have this template: <?xml version="1.0" encoding="utf-8"?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Japanese.OpenPageTemplate" x:Name="this"

How do I write a JSON file in C#?

callumdisney: I'm making games in Unity. I'm trying to make an options menu, but my apply button doesn't work. I want it so options are written to a JSON file. I have tried this but I get this error: Assets/Scripts/SettingsManager.cs(48,39): error CS0117: 'Jso

How do I write a JSON file in C#?

callumdisney: I'm making games in Unity. I'm trying to make an options menu, but my apply button doesn't work. I want it so options are written to a JSON file. I have tried this but I get this error: Assets/Scripts/SettingsManager.cs(48,39): error CS0117: 'Jso

How do I write a JSON file in C#?

callumdisney: I'm making games in Unity. I'm trying to make an options menu, but my apply button doesn't work. I want it so options are written to a JSON file. I have tried this but I get this error: Assets/Scripts/SettingsManager.cs(48,39): error CS0117: 'Jso

How do I call a C++ function from a JSP

username I have called C++ functions from Java by using Java native interfaces, but now I want to use these Java native interfaces in JSP. So how to use Java native interface in Java web project? David Ehrmann You didn't know that JSP is used for templates, no

How do I call a C++ function from a JSP

username I have called C++ functions from Java by using Java native interfaces, but now I want to use these Java native interfaces in JSP. So how to use Java native interface in Java web project? David Ehrmann You didn't know that JSP is used for templates, no

How do I write a test method for the createNewToken method?

Because Usel I'm new to this Mockito J-Unit test and I want to test my createNewToken()-Method but I can't understand its logic. Here is my method: public String createNewToken(String usertoken) { WebToken oldToken = getTokenByUserToken(usertoken); if

How do I correctly call the method when I know the class?

Mo Fubo I'm implementing an IdChecker program that checks the uniqueness of the "id" of an item's classes and methods. IdChecker only runs when my project compiles...before I release a build. For classes, it's easy to get the ID, because I just need to map all