Timeout error on HTTPoison

Troubleshooting - I’m getting timeout error on this code when called using rails application, but works fine when called from Postman

defp auth_api_call(config) do
  url = "#{config["host_url"]}#{config["auth_params"]["route"]}"
  headers = [{"Content-type", "application/json"}]
  auth_params = config["auth_params"] |> Map.take(~w(username password))

  body = Poison.encode!(auth_params)

  with {:ok, %HTTPoison.Response{body: body}} <- HTTPoison.post(url, body, headers) do
    body |> Poison.decode!
  else
    {:error, %HTTPoison.Error{reason: reason}} ->
      IO.inspect reason  
  end
end

Thanks!

Maybe the problem is with the rails app?

How are you calling that from a rails app? What is postman?

Where do you get the timeouts? In Rails or in that elixir code?

Postman is a chrome extension that allows You to make api request.

It means probably that the api is ok, and the problem might be elsewhere, but who knows…

BTW Are You sure to put the auth_params in the body? Check with what You pass to postman.

The call stack is like this

Rails App -> Phoenix App -> Calls Auth API

The timeout occurs when Phoenix App calls the auth api from this code

defp auth_api_call(config) do
  url = "#{config["host_url"]}#{config["auth_params"]["route"]}"
  headers = [{"Content-type", "application/json"}]
  auth_params = config["auth_params"] |> Map.take(~w(username password))

  body = Poison.encode!(auth_params)

  with {:ok, %HTTPoison.Response{body: body}} <- HTTPoison.post(url, body, headers) do
    body |> Poison.decode!
  else
    {:error, %HTTPoison.Error{reason: reason}} ->
      IO.inspect reason  
  end
end

From Postman(API testing Extension for Chrome) I send the same params as that from the Rails app.
But the post call from HTTPoison to auth api fails in case of rails app.
So was checking for a solution to extend the timeout. Maybe something wrong with this code.

HTTPoison.post!(url, body, headers, [recv_timeout: 5000])

Thanks

Why do you think you get the timeout in httpoison and not in your rails app? Can you show us the logs of the error from both Phoenix and rails?

[info] Sent 500 in 5360ms
[error] #PID<0.421.0> running ProcessorApiWeb.Endpoint terminated
Server: 0.0.0.0:4000 (http)
Request: POST /api/process/import
** (exit) an exception was raised:
** (HTTPoison.Error) :timeout
(httpoison) lib/httpoison.ex:66: HTTPoison.request!/5
(processor_api) lib/processor_api/processor/processor.ex:39: ProcessorApi.Processor.auth_api_call/1
(processor_api) lib/processor_api/processor/processor.ex:11: ProcessorApi.Processor.import_order_csv/1
(processor_api) lib/processor_api_web/controllers/process_controller.ex:11: ProcessorApiWeb.ProcessController.import/2
(processor_api) lib/processor_api_web/controllers/process_controller.ex:1: ProcessorApiWeb.ProcessController.action/2
(processor_api) lib/processor_api_web/controllers/process_controller.ex:1: ProcessorApiWeb.ProcessController.phoenix_controller_pipeline/2
(processor_api) lib/processor_api_web/endpoint.ex:1: ProcessorApiWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:278: Phoenix.Router.call/1
(processor_api) lib/processor_api_web/endpoint.ex:1: ProcessorApiWeb.Endpoint.plug_builder_call/2

Are You testing

Rails App -> Phoenix App (with postman)

and this part is failing with timeout?

Phoenix App -> Calls Auth API

Maybe You can test Phoenix App -> Calls Auth API with postman, and check also You are not flooding the API.

If not, maybe Rails is the guilty part?

Found the issue

Since rails is running in development mode, it is not able to receive multiple request. Which results in timeout errors.
So the auth api calls back the rails code again, for auth data to be inserted in DB. Which creates a cycle not possible to run on single thread.

Really appreciate your help on this issue.

Thanks Guys

2 Likes