Ani*_*ena 3 python json haskell
我有一个接受 json 请求的服务器,它们可以与 python 客户端一起正常工作。我正在尝试在 haskell 中做同样的事情。
例如我的python客户端有以下代码
conn = JSONRPCProxy("XXX.XX.XX.XX", 5050, "2.0", 120)
print conn.request('show_attrs', [{"shortcode":"CODEWORD1","fx":'CD2'}, ["?queryattr", "queryname"]]);
Run Code Online (Sandbox Code Playgroud)
我想使用 haskell 发送相同的查询并返回 json 作为结果。现在不用担心 json 解析!
我试过 Network.Wreq,它有点工作,但总是没有响应
data MyPriceRequest = MyRequestRequest { method::String,id::Int} deriving (Show,Generic)
instance ToJSON MyPriceRequest
r <- post "http://IP.IP.IP.IP:port/" (toJSON (MyRequestRequest "codeword" [""] 1))
*** Exception: NoResponseDataReceived
Run Code Online (Sandbox Code Playgroud)
我得到异常,但相同的代码适用于 python
在python中它适用于...
conn = JSONRPCProxy("IP.IP.IP.IP", port, "2.0", 120)
print conn.request('codeword', []);
Run Code Online (Sandbox Code Playgroud)
这是一个POST对进行查询的工作示例httpbin.org:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Wreq
import Control.Lens
import Data.Aeson.Lens
import qualified Data.Text as T
import Data.Aeson
main = do
r <- post "http://httpbin.org/post" (toJSON (T.unpack "123"))
let datas = r ^. responseBody . key "data" . _String
ua = r ^. responseBody . key "headers" . key "User-Agent" . _String
(putStrLn . T.unpack) datas
(putStrLn . T.unpack) ua
Run Code Online (Sandbox Code Playgroud)
它应该打印如下内容:
"123"
haskell wreq-0.3.0.1
Run Code Online (Sandbox Code Playgroud)
我用表单的 URL 尝试了它,http://127.0.0.1:80它给出了相同的结果。
你有没有试过curl在你的 shell 中使用它来查看它是否表现出相同的行为?服务器是否可能仅在某些标头字段存在或设置为特定值时才应答?