1

I'm writing a REST API for a resource in Yesod. I have a POST method that should try to create the given resource and if successful return 201 and the ID of the newly created resource. E.g.,

postResourceR :: Handler String
postResourceR = do
  id <- runDB $ insert Resource
  sendResponseStatus status201 id -- DOES NOT COMPILE

Here's the error I get, which is plain enough:

No instance for (ToTypedContent (Key Resource))
  arising from use of 'sendResponseStatus'

I know I want something of the ToTypedContent, but I can't find any way to get that from a Key in Database.Persist.Class. Any help would be greatly appreciated.

EDIT:

Here's the definition of the actual resource:

Notification
    title Text
    content Text
    icon Text Maybe
    action Text Maybe 
    created UTCTime
    updated UTCTime
    deriving Show
imperfectgrist
  • 621
  • 4
  • 20
  • 1
    AFAIK `ToTypedContent` is basically a `ByteString`([see the Yesod book/docu](http://www.yesodweb.com/book/restful-content)) - it's a hard to tell how you could get it because we don't even know the type of your `Key` – Random Dev Apr 02 '15 at 06:41
  • Sorry, I didn't think it was relevant because i didn't explicitly define it. The definition of my resource is now added. – imperfectgrist Apr 02 '15 at 07:07
  • 1
    the concrete type will also depend on your db-backend etc. - anyway did you have a look at the yesod book I linked? Basically if you can `show` the key just `show id` should work - although I guess you sooner or later need the other direction as well – Random Dev Apr 02 '15 at 07:20
  • I've read the chapter, but I couldn't put it together. I see it now in the definition of `ToContent` and `ToTypedContent`. Thanks! – imperfectgrist Apr 02 '15 at 08:06

1 Answers1

5

Presuming you're using a SQL backend, you can use fromSqlKey. More generally you can use toBackendKey.

Michael Snoyman
  • 31,100
  • 3
  • 48
  • 77