servant icon indicating copy to clipboard operation
servant copied to clipboard

UTCTime in url results in 'not found' error

Open rubenmoor opened this issue 1 year ago • 2 comments

I have this route:

type RoutesCMS =
          Capture "system"   SystemLang
       :> Capture "lang"     TextLang
       :> Capture "pagename" Text
       :> Capture "time"     UTCTime
       :> Get '[JSON] [Pandoc]

Just once I added Capture "time" UTCTime, I get a not found error for my api:

No handler accepted "/api/cms/SystemDE/EN/Stage1-1_Type-the-letters/1859-02-25T00%3A16%3A40Z"

I changed UTCTime to a newtype with custom instances for FromHttpApiData and ToHttpApiData like this:

newtype UTCTimeInUrl = UTCTimeInUrl { unUTCTimeInUrl :: UTCTime }

instance ToHttpApiData UTCTimeInUrl where
  toUrlPiece = Text.replace ":" "_" . toUrlPiece . unUTCTimeInUrl

instance FromHttpApiData UTCTimeInUrl where
  parseUrlPiece = fmap UTCTimeInUrl . parseUrlPiece . Text.replace "_" ":"

and the problem vanishes. So there seems to be a problem with the url encoding of ":".

I am using Servant 0.19.1 and http-api-data 0.4.2 (the newest version is 0.5). I am unsure whether or not this issue belongs to the repo of http-api-data. I don't know where the url encoding happens.

rubenmoor avatar Mar 16 '23 00:03 rubenmoor

how is your URL getting encoded? the URL that successfully gets captured is /api/cms/SystemDE/EN/Stage1-1_Type-the-letters/1859-02-25T00:16:40Z (i.e. without the %3As). http-api-data-0.5 has some adjustments to how years with less than four digits are parsed, but this shouldn't make a difference in your case

intolerable avatar Apr 04 '23 16:04 intolerable

Oh I see. The ":" shouldn't be url encoded in the first place. So probably this is a bug with servant-reflex (which I use to generate my client). I will check there ...

rubenmoor avatar Apr 04 '23 16:04 rubenmoor

@rubenmoor Can we close this ticket on our end? Is this something that servant can still act on?

tchoutri avatar Apr 25 '24 17:04 tchoutri

please close, definitely a problem specific to servant-reflex

ruben-benaco avatar Apr 25 '24 23:04 ruben-benaco