stephenbenedict
stephenbenedict

Reputation: 125

How to set HTTP status code manually in IHP response

Context

I am using EasyCron (https://www.easycron.com) to ping an endpoint in my IHP app.

If the code on my app's side fails for some reason, I would like to set the HTTP status code to 503 or similar and display the errors in the response so that,

  1. the cron job is marked as failed on EasyCron and
  2. I can see the relevant IHP errors in EasyCron's cron job records.

Since my response is not going to be parsed by EasyCron, I don't need it to be in JSON format. It's just going to be saved in the logs.

Question

I see that there is renderJsonWithStatusCode but since I have no need to parse the JSON and it is extra work to create JSON instances of my data, I would prefer to render a plain text response but with a custom status code. Is this possible?

Upvotes: 1

Views: 65

Answers (1)

Marc Scholten
Marc Scholten

Reputation: 1396

You can set a custom status code by calling IHP's respondAndExit with a custom response:

import Network.Wai (responseLBS)
import Network.HTTP.Types (status503)
import Network.HTTP.Types.Header (hContentType)

action MyAction = do
    let text = "An error happend"
    respondAndExit $ responseLBS status503 [(hContentType, "text/plain")] text

Upvotes: 2

Related Questions