HTTPOISON-在elixir中插入身体参数
我正在尝试执行http请求HTTPOISON-在elixir中插入身体参数,http,elixir,Http,Elixir,我正在尝试执行http请求 def getPage() do url = "http://myurl" body = '{ "call": "MyCall", "app_key": "3347249693", "param": [ { "page" : 1,
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
这对我很有效
我的问题是-如何在body请求中插入变量。
意思是:
当我运行它时,我得到
%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:...
有什么建议吗?您需要输入以下值:
body = '{
"call": "MyCall",
"app_key": "#{key}",
"param": [
{
"page" : #{page},
"registres" : "#{registres}",
"filter" : "#{filter}"
}
]
}'
如果您使用JSON库(Poison是一种流行的选择),那么您可以这样做,将Elixir数据结构转换为JSON表示:
body = %{
call: "MyCall",
app_key: key,
param: [
{
page: page,
registres: registers,
filter: filter
}
]
} |> Poison.encode!()
您真的应该为此使用类似的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, [])
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, [])