Hi Elixir Community,
My Phoenix app has set up swagger and it works with Get, Update, Delete, get by ID requests but not with Post request
my create() in my controller looks like this
def create(conn, %{"campaign" => campaign_params}) do
with {:ok, %Campaign{} = campaign} <- Store.create_campaign(campaign_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.campaign_path(conn, :show, campaign))
|> render("show.json", campaign: campaign)
end
end
So I believe that means It accepts a json like this
or this:
I tried both ways and I got 422 error code ( I coded it manually in fallback_controller)
Meanwhile, for my update endpoint:
I used :
and it works just fine:
My update() is just like my create() which accept a “Campaign” => campaign_params:
def update(conn, %{"id" => id, "campaign" => campaign_params}) do
campaign = Store.get_campaign!(id)
with {:ok, %Campaign{} = campaign} <- Store.update_campaign(campaign, campaign_params) do
render(conn, "show.json", campaign: campaign)
end
end
Any thoughts ? Thank you
My swagger definition in my controller:
def swagger_definitions do
%{
campaign:
swagger_schema do
title("Campaign")
description("A campaign.")
properties do
data(
Schema.new do
properties do
name(:string, "Campaign name", required: true)
start_time(:string, "Start time of the campaign.",
required: true
)
end_time(:string, "End time of the campaign.",
required: true
)
budget(:integer, "Budget of the campaign.",
required: true
)
hashtags(:string, "Hashtags of the campaign.",
required: true
)
team_id(:integer, "Team_id of the campaign.",
required: true
)
description(:string, "Description of the campaign.",
required: true
)
end
end
)
end
example(%{
campaign: %{
name: "Marketing 101",
start_time: "21/1/2020 12:00:00",
end_time: "1/14/2020 12:00:00",
budget: 6000,
hashtags: "#hashtags",
team_id: 4,
description: "Description"
}
})
end
end