在URL中输入参数后,我已经收到POST和GET请求,它们可以工作。但是,发布JSON有效负载会导致我的端点超时,并且api网关日志对调试正在发生的事情毫无用处。这是我在仆人中的API:
type ItemApi = "updateitem" :> ReqBody '[JSON] UpdateItem :> Post '[JSON] String
data UpdateItem = UpdateItem {itmId :: String}
instance FromJSON UpdateItem where
parseJSON = withObject "UpdateItem" $ \v -> do
id <- v .: "itmId"
return $ UpdateItem id
updateItemHandler :: UpdateItem -> Handler String
updateItemHandler (UpdateItem _) = do
liftIO $ updateItem 3 999
mItem <- liftIO $ checkForExistingItem 3
liftIO $ print "Updated"
liftIO $ print $ show mItem
return "Return Updated"
updateItem :: Int -> Int -> IO ()
updateItem itemId amt = do
conn <- connect connectIn
mItem <- checkForExistingItem itemId
case mItem of
Just itm -> do runBeamPostgres conn $ runUpdate $ save (_shoppingItem shoppingCartDb)
$ itm { _itemAmount = amt }
mItem <- checkForExistingItem itemId
print $ show mItem
close conn
Nothing -> do print $ "Item id: " <> show itemId <> " does not exist"
close conn
checkForExistingItem :: Int -> IO (Maybe Item)
checkForExistingItem id = do
conn <- liftIO $ connect connectIn
mTalEntry <-
runBeamPostgres conn
$ runSelectReturningOne
$ select
$ checkForExistingItemExpression id
close conn
return mTalEntry
checkForExistingItemExpression
:: Int -> Q Postgres ShoppingCartDb s (ItemT (QExpr Postgres s))
checkForExistingItemExpression id =
filter_ (\item -> _itemId item ==. (val_ id))
$ all_ (_shoppingItem shoppingCartDb)
我正在使用Postman使用以下有效负载测试我的请求:
{ "itmId": "test" }
这是我的serverless.yml文件:
service: serverless-minimal-example-final
provider:
name: aws
runtime: haskell
region: us-east-1
timeout: 30
functions:
apigw:
handler: serverless-haskell-example.apigw
events:
- http:
path: updateitem
method: post
plugins:
- serverless-haskell
- serverless-offline