我正在尝试发出http请求
def getPage() do
url = "http://myurl"
body = '{
"call": "MyCall",
"app_key": "3347249693",
"param": [
{
"page" : 1,
"registres" : 100,
"filter" : "N"
}
]
}'
headers = [{"Content-type", "application/json"}]
HTTPoison.post(url, body, headers, [])
end
这对我很好。
我的问题是-如何在正文请求中插入变量。
意义:
def getPage(key, page, registers, filter) do
url = "http://myurl"
body = '{
"call": "MyCall",
"app_key": key,
"param": [
{
"page" : page,
"registres" : registers,
"filter" : filter
}
]
}'
headers = [{"Content-type", "application/json"}]
HTTPoison.post(url, body, headers, [])
end
当我运行它时,我得到
%HTTPoison.Response{body: "\nFatal error: Uncaught exception 'Exception' with message 'Invalid JSON object' in /myurl/www/myurl_app/api/lib/php-wsdl/class.phpwsdl.servers.php:...
有什么建议么?
最佳答案
您确实应该为此使用Poison这样的JSON编码器。
url = "http://myurl"
body = Poison.encode!(%{
"call": "MyCall",
"app_key": key,
"param": [
%{
"page": page,
"registres": registers,
"filter": filter
}
]
})
headers = [{"Content-type", "application/json"}]
HTTPoison.post(url, body, headers, [])
关于HTTPOISON-在Elixir中插入主体参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37385314/