2016-07-12 77 views
2

我試圖編寫一些東西來做一些json API的請求。 我有這樣的功能:WReq簡單的發佈請求

apiLogin = do 
    r <- post "https://url:port/somewhere" ["user" := "some_name", "password" := "test"] 
    putStrLn $ cs $ r ^. responseBody 

下面是我得到的錯誤:

Api.hs:12:96: 
No instance for (FormValue v0) arising from a use of ‘:=’ 
The type variable ‘v0’ is ambiguous 
Note: there are several potential instances: 
    instance FormValue a => FormValue (Maybe a) 
    -- Defined in ‘Network.Wreq.Types’ 
    instance FormValue GHC.Int.Int16 -- Defined in ‘Network.Wreq.Types’ 
    instance FormValue GHC.Int.Int32 -- Defined in ‘Network.Wreq.Types’ 
    ...plus 18 others 
In the expression: "user" := "some_name" 
In the second argument of ‘post’, namely 
    ‘["user" := "some_name", "password" := "test"]’ 
In a stmt of a 'do' block: 
    r <- post 
     "https://url:port/somewhere/g" 
     ["user" := "some_name", "password" := "test"] 

Api.hs:12:99: 
No instance for (Data.String.IsString v0) 
    arising from the literal ‘"some_name"’ 
The type variable ‘v0’ is ambiguous 
Note: there are several potential instances: 
    instance Data.String.IsString 
      http-client-0.4.29:Network.HTTP.Client.Types.Request 
    -- Defined in ‘http-client-0.4.29:Network.HTTP.Client.Request’ 
    instance Data.String.IsString 
      http-client-0.4.29:Network.HTTP.Client.Types.RequestBody 
    -- Defined in ‘http-client-0.4.29:Network.HTTP.Client.Types’ 
    instance (a ~ Data.ByteString.Internal.ByteString) => 
      Data.String.IsString 
      (attoparsec-0.13.0.2:Data.Attoparsec.ByteString.Internal.Parser a) 
    -- Defined in ‘Data.Attoparsec.ByteString.Char8’ 
    ...plus 11 others 
In the second argument of ‘(:=)’, namely ‘"some_name"’ 
In the expression: "user" := "some_name" 
In the second argument of ‘post’, namely 
    ‘["user" := "some_name", "password" := "test"]’ 

Api.hs:12:122: 
No instance for (FormValue v1) arising from a use of ‘:=’ 
The type variable ‘v1’ is ambiguous 
Note: there are several potential instances: 
    instance FormValue a => FormValue (Maybe a) 
    -- Defined in ‘Network.Wreq.Types’ 
    instance FormValue GHC.Int.Int16 -- Defined in ‘Network.Wreq.Types’ 
    instance FormValue GHC.Int.Int32 -- Defined in ‘Network.Wreq.Types’ 
    ...plus 18 others 
In the expression: "password" := "test" 
In the second argument of ‘post’, namely 
    ‘["user" := "some_name", "password" := "test"]’ 
In a stmt of a 'do' block: 
    r <- post 
     "https://url:port/somewhere/g" 
     ["user" := "some_name", "password" := "test"] 

Api.hs:12:125: 
No instance for (Data.String.IsString v1) 
    arising from the literal ‘"test"’ 
The type variable ‘v1’ is ambiguous 
Note: there are several potential instances: 
    instance Data.String.IsString 
      http-client-0.4.29:Network.HTTP.Client.Types.Request 
    -- Defined in ‘http-client-0.4.29:Network.HTTP.Client.Request’ 
    instance Data.String.IsString 
      http-client-0.4.29:Network.HTTP.Client.Types.RequestBody 
    -- Defined in ‘http-client-0.4.29:Network.HTTP.Client.Types’ 
    instance (a ~ Data.ByteString.Internal.ByteString) => 
      Data.String.IsString 
      (attoparsec-0.13.0.2:Data.Attoparsec.ByteString.Internal.Parser a) 
    -- Defined in ‘Data.Attoparsec.ByteString.Char8’ 
    ...plus 11 others 
In the second argument of ‘(:=)’, namely ‘"test"’ 
In the expression: "password" := "test" 
In the second argument of ‘post’, namely 
    ‘["user" := "some_name", "password" := "test"]’ 

我不知道我做錯了,我試圖做同樣的事情,他們在文檔中顯示(http://www.serpentine.com/wreq/tutorial.html通過郵件上傳數據),除非不在ghci中。我正在使用重載的字符串。 有人可以解釋這些錯誤的含義嗎?

+0

該錯誤是因爲對函數':='的歧義。如果您指定名稱和值都是'ByteString',則代碼將運行。不過,我不確定爲什麼它能解決GHCI中的歧義問題,但不能解決獨立代碼中的歧義問題。 –

+0

哈是的,謝謝!這就是它 – Ulrar

+0

我有同樣的問題,你在代碼中做了什麼修改來解決這個問題? – ManzMoody

回答

1

錯誤是因爲對函數:=的歧義。如果您指定名稱和值都是ByteString,則代碼將運行。

我不確定爲什麼它解決了GHCI中的歧義,但不是在獨立代碼中。