Skip to content

Instantly share code, notes, and snippets.

@lyo5ha
Created December 11, 2018 10:01
Show Gist options
  • Save lyo5ha/26bd10d55c7810dc28a5cd389658c543 to your computer and use it in GitHub Desktop.
Save lyo5ha/26bd10d55c7810dc28a5cd389658c543 to your computer and use it in GitHub Desktop.
phoenix
# В контроллере вызывается код, который через HTTPoison делает POST-запрос
# на сторонний сервис. Сервис отвечает 302 редиректом. Этот редирект обернут в HTTPoison response
# и у пользователя в браузере редиректа не происходит. Как сделать так, чтобы произошел редирект?
# (Т.е. понятно, что нужно, чтобы функция вернула conn, а не HTTPoison response, как это сделать?
## controller
...
def create(conn, %{"payment" => payment_params}) do
case Transactions.create_payment(payment_params) do
{:ok, payment} ->
PaymentCreation.create_request(payment)
|> # сюда передается результат send_post_request (см ниже)
# в виде стракта HTTPoison response с 302 редиректом.
# Как этот 302 редирект от HTTPoison "положить" в conn
# и сделать так, чтобы редирект сработал из контроллера?
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
...
# модуль PaymentCreation, который шлет POST запрос, на который сервис отвечает 302 редиректом
...
def send_post_request(list) do
[url | [body | [headers | _etc]]] = list
HTTPoison.request(:post, url, body, headers, follow_redirect: true, force_redirect: true)
end
...
# структура, которую возвращает HTTPoison в ответ на POST
{:ok,
%HTTPoison.AsyncResponse{
id: {:maybe_redirect, 302,
[
{"Date", "Mon, 10 Dec 2018 16:56:37 GMT"},
{"Content-Type", "text/html; charset=utf-8"},
{"Content-Length", "5576"},
{"Connection", "keep-alive"},
{"Set-Cookie",
"__cfduid=<здесь какой-то ключ>; expires=Tue, 10-Dec-19 16:56:37 GMT; path=/; domain=<домен>.com; HttpOnly"},
{"Cache-Control", "no-store, no-cache"},
{"Location", "/Account/logout?url=%2Fmerchant%2Fprepare"},
{"Set-Cookie",
".epayments_session=<ключ сессии>; domain=<домен>.com; path=/; secure; HttpOnly"},
{"Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE"},
{"Access-Control-Allow-Headers",
"authorization,content-Type,accept-language,accept,x-device"},
{"X-Frame-Options", "<домен>.com"},
{"Content-Security-Policy",
"default-src 'self' 'unsafe-inline' *.google.com *.googlecode.com *.ecommpay.com; script-src 'unsafe-inline' 'self' *.google.com *.googlecode.com jpillora.com *.ecommpay.com; connect-src: 'self'; img-src: 'self' *.google.com *.ecommpay.com; style-src: 'self'"},
{"X-Content-Type-Options", "nosniff"},
{"X-XSS-Protection", "1; mode=block"},
{"Strict-Transport-Security",
"max-age=15768000; includeSubdomain; preload; always"},
{"X-Request-ID", "<айди>"},
{"Referrer-Policy", "origin"},
{"Expect-CT",
"max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""},
{"Server", "cloudflare"},
{"CF-RAY", "<какой-то айди>"}
],
{:client, {1544, 460997, 87470}, {:metrics_ng, :metrics_dummy},
:hackney_ssl, '<домен>.com', 443,
"<домен>.com", [follow_redirect: true],
{:sslsocket, {:gen_tcp, #Port<0.139>, :tls_connection, :undefined},
[#PID<0.697.0>, #PID<0.696.0>]},
{:default, #Reference<0.3618027905.1729626115.40663>,
{'<домен>.com', 443, :hackney_ssl}, #PID<0.469.0>,
:hackney_ssl}, #Reference<0.3618027905.1729626115.40663>, true,
:hackney_pool, 5000, true, 5, false, 5, nil, nil,
{:hparser, :response, 4096, 10, 0, :on_body, "<html><head><script type=\"",
{1, 1}, "", [], 5576, "", "keep-alive", "text/html; charset=utf-8",
"/Account/logout?url=%2Fmerchant%2Fprepare", :waiting},
{20,
{:dict, 19, 16, 16, 8, 80, 48,
{[], [], [], [], [], [], [], [], [], [], [], ...},
{{[["content-length", {2, "Content-Length", "5576"}]],
[
[
"location",
{6, "Location", "/Account/logout?url=%2Fmerchant%2Fprepare"}
],
[
"strict-transport-security",
{14, "Strict-Transport-Security",
"max-age=15768000; includeSubdomain; preload; always"}
]
],
[
["date", {0, "Date", "Mon, 10 Dec 2018 16:56:37 GMT"}],
["x-content-type-options", {12, "X-Content-Type-Options", ...}],
["expect-ct", {17, ...}],
["cf-ray", {...}]
], [], [], [["access-control-allow-methods", {...}], ["server", ...]],
[["x-request-id", ...]], [[...], ...], [], ...}}}}, :connected,
:waiting, nil, :normal, false, false, false, :undefined, false,
&:hackney_request.send/2, :waiting, nil, 4096, "", [], {1, 1}, 5576, nil,
nil, "POST", "/merchant/prepare", ...}}
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment