我正在尝试使用Haskell Servant复制此curl请求
curl -v -H 'Accept: application/vnd.twitchtv.v5+json' \
-H 'Client-ID: someapikey' \
-X GET 'https://api.twitch.tv/kraken/clips/top?game=Overwatch&period=week&trending=false&limit=3'
使用Twitch API。文件here
这是我到目前为止所得到的
type Game = Text
type Cursor = Text
type Language = Text
type Limit = Int
type Period = Text
type Trending = Bool
type Application = Text
type ClientID = Text
type SearchClips = "kraken"
:> "clips"
:> "top"
:> QueryParam "game" Game
:> QueryParam "cursor" Cursor
:> QueryParam "language" Language
:> QueryParam "limit" Limit
:> QueryParam "period" Period
:> QueryParam "trending" Trending
:> Header "Accept" Application
:> Header "Client-ID" ClientID
:> Get '[JSON] Search
searchClipAPI :: Proxy SearchClips
searchClipAPI = Proxy
search
:: Maybe Game
-> Maybe Cursor
-> Maybe Language
-> Maybe Limit
-> Maybe Period
-> Maybe Trending
-> Maybe Application
-> Maybe ClientID
-> ClientM Search
search = client searchClipAPI
baseURL :: BaseUrl
baseURL = BaseUrl Https "api.twitch.tv" 443 ""
这就是我使用http-tls“运行”它的方式
runGameClipsSearchClient :: Maybe Text -> IO ()
runGameClipsSearchClient game = do
mn <- NT.newTlsManager
let args = search
game
(Just "")
(Just "en")
(Just 50)
(Just "day")
(Just False)
(Just "application/vnd.twitchtv.v5+json")
(Just "someapikey")
envClient = mkClientEnv mn baseURL
pPrint =<< runClientM args envClient
但是我一定做错了,因为在haskell代码中出现了“ 404:未找到”错误,但是在curl请求中却没有。
我怀疑标题有问题,因为删除
'Accept: application/vnd.twitchtv.v5+json'
从curl请求中,我得到完全相同的响应。
答案 0 :(得分:2)
是的,servant-client
对Accept
和Content-Type
标头有特殊处理-如果您尝试使用:> Header ...
机制将其包括在内,则会将它们从requestToClientRequest
中的函数Servant.Client.Internal.HttpClient
中的请求。
我认为强制使用Accept
标头的最简单方法是向您的Manager
添加请求处理功能。这绝对是一个丑陋的hack,但它似乎可以工作。 (我没有要测试的Twitch客户端ID,但是有了此修复程序,我得到了400错误而不是404错误,我认为这是进步的。)
mn <- NT.newTlsManagerWith (NT.tlsManagerSettings { managerModifyRequest = fixAccept })
...
where fixAccept req
= return $ req { requestHeaders = ("Accept", "application/vnd.twitchtv.v5+json") :
filter (("Accept" /=) . fst) (requestHeaders req) }