Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTTPOISON-在elixir中插入身体参数_Http_Elixir - Fatal编程技术网

HTTPOISON-在elixir中插入身体参数

HTTPOISON-在elixir中插入身体参数,http,elixir,Http,Elixir,我正在尝试执行http请求 def getPage() do url = "http://myurl" body = '{ "call": "MyCall", "app_key": "3347249693", "param": [ { "page" : 1,

我正在尝试执行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
这对我很有效

我的问题是-如何在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, [])