Respond with both body and status code in Nancy

前端 未结 4 1683
花落未央
花落未央 2020-12-29 20:23

I\'m new to Nancy and I want to return both a custom HttpStatusCode and a body (content). If I return an HttpStatusCode, it returns it with a blank body. If I return a str

相关标签:
4条回答
  • 2020-12-29 21:07

    This should work.

    public class SendSMS : NancyModule
    {
       public SendSMS()
       {
           Post["/SendSMS"] = parameters =>
           {
               return Negotiate.WithModel("Missing \"to\" param")
                               .WithStatusCode(HttpStatusCode.BadRequest)           
           };
       }
    } 
    

    For more information check the docs on controlling content negotiation.

    0 讨论(0)
  • 2020-12-29 21:18

    You could always create an instance of the Response type and set the Body and StatusCode yourself. If you wanted to take a shortcut you could do something like

    var r = (Response)"Some string that goes into the body";
    r.StatusCode = 123;
    
    return r;
    
    0 讨论(0)
  • 2020-12-29 21:20

    This is the simplest way I've found:

    Return from your Module:

    return new Response {
                    StatusCode = HttpStatusCode.NotFound, ReasonPhrase = "Resource not found"
                };
    
    0 讨论(0)
  • 2020-12-29 21:30

    If you have problems with encoding it's better to use

    return new TextResponse(HttpStatusCode.STATUS, "Text Responsé")
    
    0 讨论(0)
提交回复
热议问题