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

hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement