使用Maru框架在Post请求中获取主体

时间:2018-06-25 08:51:37

标签: elixir maru

我是Elixir的新手。 我正在发送带有application / json主体的POST请求。我正在使用邮递员。当我通过POST请求发送JSON主体有效负载时,出现服务器错误。即:

%Plug.Parsers.ParseError{
  exception: %UndefinedFunctionError{
    arity: 1,
    exports: nil,
    function: :decode!,
    module: Poison,
    reason: nil
  },
  plug_status: 400
}

下面是请求,请求的代码以及处理该请求的路由。我不知道我在做什么错。

顺便说一句:当我以Content-Type:text / plain发送消息时,请求有效,但在任何地方都找不到正文。

我想要的是在body参数中获取JSON。

OST /api/v1/users/abs22/devices/ytuh48753hhkjfo/power HTTP/1.1
Host: localhost:8880
-X-App-Id: Balalalal
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 0eaf390c-37e5-88db-c1c7-e05c7dbfbf01

{
    "device": {
        "power_on": true
    }
}

路由器看起来像这样:

  namespace :devices do

    params do
      group :device, type: Json |> List do
        optional :power_on, type: Boolean
      end
    end
    get do
      conn
      |> json(
        %{
          devices: [
                   %{
                     uuid: :abcs,
                     name: :string,
                     description: :string,
                     is_online: true,
                     is_on: false,
                     has_current: true,
                     site_id: nil,
                     user_id: conn.params["user_id"]

                   },
                   %{
                     uuid: :abcf,
                     name: :string,
                     description: :string,
                     is_online: true,
                     is_on: false,
                     has_current: true,
                     site_id: nil,
                     user_id: conn.params["user_id"]

                   }
                 ]
               }
      )
    end

    post do
      conn
      |> json(
        %{
          device: %{
          uuid: :abcf,
          name: :string,
          description: :string,
          is_online: true,
          is_on: false,
          has_current: true,
          site_id: nil,
          user_id: conn.params["user_id"]
          }
        }
      )
    end

    route_param :device_id, type: String do
      get do
        conn
        |> json(
          %{
            device: %{
              uuid: params[:device_id],
              name: :string,
              description: :string,
              is_online: true,
              is_on: false,
              has_current: true,
              site_id: nil,
              user_id: conn.params["user_id"],
              timers: [
                %{
                  uuid: :string,
                  description: "10 byte representing switch, week, days, time on, time off",
                  kind: "reccuring"

                },
                %{
                  uuid: :string,
                  description: "10 byte representing switch, week, days, time on, time off",
                  kind: "countdown"

                },
                %{
                  uuid: :string,
                  description: "10 byte representing switch, week, days, time on, time off",
                  kind: "pattern"

                },
              ]
            }
          }
        )
      end

      put do
        conn
        |> json(
          %{
            device: %{
              uuid: params[:device_id],
              name: :change1,
              description: :change2,
              is_online: true,
              is_on: false,
              has_current: true,
              site_id: nil,
              user_id: conn.params["user_id"]
            }
          }
        )
      end

      delete do
        conn
        |> json(
          %{
            device: %{
              uuid: params[:device_id],
              name: :string,
              description: :string,
              is_online: true,
              is_on: false,
              has_current: true,
              site_id: nil,
              user_id: conn.params["user_id"]
            }
          }
        )
      end

      post :power do
        conn
        |> IO.inspect
        |> json(
          %{
            device: %{
              uuid: params[:device_id],
              name: :string,
              description: :string,
              is_online: true,
              is_on: true,
              has_current: true,
              site_id: nil,
              user_id: conn.params["user_id"]
            }
          }
        )

      end
    end
  end

0 个答案:

没有答案