undeadko

undeadko

HTTPoison request with authentication

Hello everyone,

I am trying to access a web page with some pictures in it, so that I can download the pictures and then do some fun things with them. Very much like this post here with the smoothies https://medium.com/@Julien_Corb/web-scraping-with-elixir-using-httpoison-and-floki-26ebaa03b076 .

The difference is, the access is gated behind a login screen → username and password. I am fairly new to all of this, so my rookie mistake was to just send a request to the link, which naturally returns to me a %HTTPoison.Response{} structure. The body of the response being the logging screen.

For some reason the documentation of Httpoison is lacking… or rather not helpful, as it is just “This is an existing function. It takes parameters.” There is no example for what I am trying to do, which I thought would be a very common thing. Should I go to Hackney to find proper documentation?

So my question is, Can I do this all in one go? - Send a request with the appropriate logging parameters and directly receiving the body / the images straight away. How? How do I know, where do I look for further help?

Much appreciated if somebody could help with this. Thank you in advance!

Most Liked

D4no0

D4no0

First of all you should inspect the login page and find the actual POST request that is done to the server when you input the username and password and press submit, you should be able to identify it in browser debug windownetwork tab, that is the request you want to use to login with a http client like HTTPoison.

The second part is a little bit more tricky, you need to save the session cookie that you get after successful login and use it in subsequent requests to protected routes, you can take some inspiration from here: Help with HTTPoison POST - #7 by Aetherus

PS: That might not be the best example as its filled with macro magic, what you are interested in are the following lines:

# Options sent to hackney for setting the request cookies
options = [hackney: [cookie: cookies]]]

# Example usage
headers = []
HTTPoison.get("my_url", headers, options)

# Extracting cookies from the response
defp cookies(%HTTPoison.Response{} = resp) do
    resp.headers
    |> Enum.filter(fn
      {"Set-Cookie", _} -> true
      _ -> false
    end)
    |> Enum.map(fn{_, cookie} -> cookie end)
  end

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement