postWith 是否正在切换我的请求的 Content-Type?
Is postWith switching my request's Content-Type?
无论我在请求 "Content-Type" 中输入什么值,我发出的传出请求似乎都将其替换为 "application/x-www-form-urlencoded"。我尝试访问的应用程序需要 "application/json"。我的代码基本上在下面。
{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
...
submissionResources = ["https://widgets.example.com/v2/widgets/request"]
sendWidgetToEndpoint submissionResources workingToken key widgetArray = do
let opts = defaults & header "Content-Type" .~ ["application/json"]
& header "apikey" .~ [key]
& header "Authorization" .~ [workingToken]
endPointURL = head submissionResources
widgetId = widgetArray !! 0
numberOfWidgets = widgetArray !! 1
widgetText = widgetArray !! 2
submissionResult <- postWith opts endPointURL [ "widgetId" := widgetId
, "numWidgets" := numberOfWidgets
, "widgetText" := widgetText
]
return submissionResult
我的问题是我一直从这个端点返回 Status {statusCode = 415, statusMessage = "Unsupported Media Type"}
,我相信这是因为我发送的请求似乎覆盖了我的 [=31] 中的 "Content-Type" =].我已经尝试使用 "application/json" 和 "text/plain" 但我收到的回复总是向我表明我发送的所有 header 看起来都符合预期,除了 Content-Type 总是有变成 "application/x-www-form-urlencoded".
如何确保 wreq 在我的请求 header 中保持 'Content-Type: application/json'?
编辑:我根据 API 服务器在回复我时告诉我的内容来确定我的原始请求中有哪些 header。
您代码段中 postWith
的最后一个参数的类型是 [FormParam]
,该类型强制对 Content-Type 进行 urlencoded。
要发送 JSON,请发送 Value
或 Encoding
类型的内容(来自 Data.Aeson
)。
import Data.Aeson (pairs, (.=))
...
-- also remove the "Content-Type" field from opts
submissionResult <- postWith opts endpointURL $ pairs
( "widgetId" .= widgetId <>
"numWidgets" .= numberOfWidgets <>
"widgetText" .= widgetText )
...
Content-Type 由您通过 Postable
实例传递给 postWith
的负载设置。如果您想使用另一个 Content-Type header,请使用 Postable
实例定义您自己的类型,您可以在其中设置适当的 Content-Type。您也可以选择不在 Postable
实例中设置任何 Content-Type,因此您可以通过选项设置它。
无论我在请求 "Content-Type" 中输入什么值,我发出的传出请求似乎都将其替换为 "application/x-www-form-urlencoded"。我尝试访问的应用程序需要 "application/json"。我的代码基本上在下面。
{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
...
submissionResources = ["https://widgets.example.com/v2/widgets/request"]
sendWidgetToEndpoint submissionResources workingToken key widgetArray = do
let opts = defaults & header "Content-Type" .~ ["application/json"]
& header "apikey" .~ [key]
& header "Authorization" .~ [workingToken]
endPointURL = head submissionResources
widgetId = widgetArray !! 0
numberOfWidgets = widgetArray !! 1
widgetText = widgetArray !! 2
submissionResult <- postWith opts endPointURL [ "widgetId" := widgetId
, "numWidgets" := numberOfWidgets
, "widgetText" := widgetText
]
return submissionResult
我的问题是我一直从这个端点返回 Status {statusCode = 415, statusMessage = "Unsupported Media Type"}
,我相信这是因为我发送的请求似乎覆盖了我的 [=31] 中的 "Content-Type" =].我已经尝试使用 "application/json" 和 "text/plain" 但我收到的回复总是向我表明我发送的所有 header 看起来都符合预期,除了 Content-Type 总是有变成 "application/x-www-form-urlencoded".
如何确保 wreq 在我的请求 header 中保持 'Content-Type: application/json'?
编辑:我根据 API 服务器在回复我时告诉我的内容来确定我的原始请求中有哪些 header。
您代码段中 postWith
的最后一个参数的类型是 [FormParam]
,该类型强制对 Content-Type 进行 urlencoded。
要发送 JSON,请发送 Value
或 Encoding
类型的内容(来自 Data.Aeson
)。
import Data.Aeson (pairs, (.=))
...
-- also remove the "Content-Type" field from opts
submissionResult <- postWith opts endpointURL $ pairs
( "widgetId" .= widgetId <>
"numWidgets" .= numberOfWidgets <>
"widgetText" .= widgetText )
...
Content-Type 由您通过 Postable
实例传递给 postWith
的负载设置。如果您想使用另一个 Content-Type header,请使用 Postable
实例定义您自己的类型,您可以在其中设置适当的 Content-Type。您也可以选择不在 Postable
实例中设置任何 Content-Type,因此您可以通过选项设置它。