1

I have been trying to create a custom happstack response the 405 "Method not allowed" so if someone calls the API with a POST or PUT method they will get this response. I am new to happstack. Any ideas how I can do that?

Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
ib1
  • 11
  • 4

1 Answers1

1

Well the ok :: (FilterMonad Response m) => a -> m a function is implemented as [src]:

ok :: (FilterMonad Response m) => a -> m a
ok = resp 200

So it is the same way like you would write an ok response, except that you should use resp :: (FilterMonad Response m) => Int -> b -> m b with a custom return code.

For example:

resp 405 "Method not allowed"

So we can for example block PUT and POST requests with something like:

main :: IO ()
main = simpleHTTP nullConf $ msum
         [ do method GET
              ok $ "This is allowed.\n"
         , do method PUT
              (resp 405) $ "Method not allowed"
         , do method POST
              (resp 405) $ "Method not allowed"
         ]
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thank you for your response , I am getting this error -- In the second argument of ‘resp’, namely ‘"Method not allowed"’ In a stmt of a 'do' block: resp 405 "Method not allowed" In the expression: do { method PUT; resp 405 "Method not allowed" } -- I tried to find what is wrong but no luck . – ib1 May 17 '18 at 19:51
  • Hmm.. I think I forgot some brackets. Edited the answer. – Willem Van Onsem May 17 '18 at 19:54
  • I am getting the same error -- • No instance for (Data.String.IsString Response) arising from the literal ‘"Method not allowed"’ • In the second argument of ‘($)’, namely ‘"Method not allowed"’ In a stmt of a 'do' block: (resp 405) $ "Method not allowed" In the second argument of ‘($)’, namely ‘do { method [PUT, POST]; (resp 405) $ "Method not allowed" }’ -- – ib1 May 17 '18 at 20:03
  • Here you include `PUT` and `POST` in the same list? – Willem Van Onsem May 17 '18 at 20:09
  • Yeah sorry wrong copy this is the one -- No instance for (Data.String.IsString Response) arising from the literal ‘"Method not allowed"’ • In the second argument of ‘($)’, namely ‘"Method not allowed"’ In a stmt of a 'do' block: (resp 405) $ "Method not allowed" In the expression: do { method PUT; (resp 405) $ "Method not allowed" } – ib1 May 17 '18 at 20:22
  • Are the there any other handlers in your list that work with a response instead of a string literal? – Willem Van Onsem May 17 '18 at 20:24
  • @ib1: in that case you need to construct as message something of the same type as the other handlers. – Willem Van Onsem May 17 '18 at 20:25
  • No there are not. – ib1 May 17 '18 at 20:26