这很可能会缺少一些重要的信息或其他信息,但是这里是。
目前,我正在尝试使用HTTP.Conduit库将自己的标题(即x-oauth-basic
)插入我的HTTP请求。它有效,但我的意图不行,
submitPostRequest urlString githubKey body =
case parseUrl urlString of
Nothing -> return $ "URL Syntax Error"
Just initReq -> withManager $ manager -> do
let req = initReq { secure = False -- Turn on https
, method = "POST"
, requestHeaders = [("x-oauth-basic", (encodeUtf8 githubKey))]
<> [("User-Agent", "HsCMS")]
, requestBody = RequestBodyBS (toStrict body)
, checkStatus = _ _ _ -> Nothing
}
res <- httpLbs req manager
return $ responseBody res
重要位是
requestHeaders = [("x-oauth-basic", (encodeUtf8 githubKey))]
<> [("User-Agent", "HsCMS")]
使用HTTP污水坑,我可以看到标头形成为HTTP_X_OAUTH_BASIC
。它不应将HTTP
位在前面。用卷发测试,
curl -u 78y8713k1j23nkjnkjnuy366366363666gdasddd:x-oauth-basic --request POST --data '{"description":"Updated via API","files":{"file1.txt":{"filename": "newsies.txt", "content":"New Demo"}}' http://www.posttestserver.com/post.php?dir=Testing
标题没有出现在那里,这表明污水坑没有拿起X头。卷曲示例还可以与我的预期端点(GitHub API)一起使用,所以我知道卷曲的方法是正确的,而我的http.conduit One也不是。
所以我的问题是,我如何使我的http.conduit标头显示为 x-header
,例如curls',而不是我得到的当前http-x-header
?
另外,不用担心,使用的github键不是实际键...
更新和修复
因此,正如对迈克尔·斯诺伊曼斯(Michael Snoymans)回答的评论中提到的那样,它通过使用其他标头(即("Authorization", "token " <> (encodeUtf8 githubKey))
)解决了,这显然在某种程度上是curl在执行<token>:x-oauth-basic
时所发送的。
我试图更新标题以使其更适合一些,但是对建议很开放...
感谢您的所有帮助!
我认为问题是您的污水坑应用程序。看起来像是打印标题的CGI版本。我不知道污水坑是什么样的,所以我在经纱中实现了一个简单的污垢,实际上,请求标头正正确地传递。您可以在FP Haskell Center上克隆该项目以自己尝试。有关完整性,以下是以下代码:
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent.Async (withAsync)
import Control.Monad.IO.Class (liftIO)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as L
import Data.Monoid (mempty, (<>))
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Network.HTTP.Conduit (RequestBody (RequestBodyBS),
checkStatus, httpLbs, method,
parseUrl, requestBody,
requestHeaders, responseBody, secure,
withManager)
import Network.HTTP.Types (status200)
import qualified Network.Wai as Wai
import Network.Wai.Handler.Warp (run)
import System.Environment (getEnv)
main :: IO ()
main = do
port <- fmap read $ getEnv "PORT"
withAsync (run port app) $ const $ do
submitPostRequest
("http://localhost:" ++ show port)
"dummy-key"
"dummy body" >>= print
app :: Wai.Application
app req = do
liftIO $ mapM_ print $ Wai.requestHeaders req
return $ Wai.responseLBS status200 [] mempty
submitPostRequest :: String -> Text -> ByteString -> IO L.ByteString
submitPostRequest urlString githubKey body =
case parseUrl urlString of
Nothing -> return $ "URL Syntax Error"
Just initReq -> withManager $ manager -> do
let req = initReq { secure = False -- Turn on https
, method = "POST"
, requestHeaders = [("x-oauth-basic", (encodeUtf8 githubKey))]
<> [("User-Agent", "HsCMS")]
, requestBody = RequestBodyBS body
, checkStatus = _ _ _ -> Nothing
}
res <- httpLbs req manager
return $ responseBody res
当我运行此操作时,控制台中的输出为:
("Host","localhost:8004")
("Accept-Encoding","gzip")
("Content-Length","10")
("x-oauth-basic","dummy-key")
("User-Agent","HsCMS")
Empty