I’m trying to automate a procedure in a Julia script which is equivalent to going on this page:
https://mercado.ren.pt/EN/Electr/MarketInfo/MarketResults/OMIE/Pages/Prices.aspx
and downloading a .xls file for the day, and I was hoping someone here had any previous successful experience in doing something similar.
Let me start by “disclaiming” that I have zero experience with webdev apart from making really basic plain html + css. My programming background is related to numerical/scientific computing, so please anticipate stupid questions/remarks.
Picture attached with the order of clicks to recreate the desired download.
After messing around a bit in the devtools on my browser, I believe the request that is performed is (for today, 27/08/2022):
“https://mercadoservices.ren.pt/api/Exports/GetExports?language=EN&nome=PrecoMerc&dia1=2022-08-27&dia2=2022-08-27&dia3=&dia4=&ordem=&tipo=”
In the browser, if I try to go directly to that link, I get
<string>API key is invalid.</string>
And in Julia, an extremely naive attempt went like
download("https://mercadoservices.ren.pt/api/PrecosMercadoSpot/GetPrecosMercadoSpotLatest?language=EN")
ERROR: HTTP/1.1 403 Forbidden while requesting https://mercadoservices.ren.pt/api/PrecosMercadoSpot/GetPrecosMercadoSpotLatest?language=EN
Stacktrace:
[1] #3
@ ~/packages/julias/julia-1.7.2/share/julia/stdlib/v1.7/Downloads/src/Downloads.jl:233 [inlined]
[2] arg_write(f::Downloads.var"#3#4"{Nothing, Vector{Pair{String, String}}, Float64, Nothing, Bool, Nothing, String}, arg::Nothing)
@ ArgTools ~/packages/julias/julia-1.7.2/share/julia/stdlib/v1.7/ArgTools/src/ArgTools.jl:101
[3] #download#2
@ ~/packages/julias/julia-1.7.2/share/julia/stdlib/v1.7/Downloads/src/Downloads.jl:221 [inlined]
[4] download(url::String, output::Nothing)
@ Downloads ~/packages/julias/julia-1.7.2/share/julia/stdlib/v1.7/Downloads/src/Downloads.jl:221
[5] #invokelatest#2
@ ./essentials.jl:716 [inlined]
[6] invokelatest
@ ./essentials.jl:714 [inlined]
[7] do_download
@ ./download.jl:24 [inlined]
[8] download(url::String)
@ Base ./download.jl:20
[9] top-level scope
@ REPL[16]:1
I believe the request on this API requires that I pass some kind of key. I have tried to google around but I am so out of my zone that I probably don’t know the correct keywords to google for. Any pointers?
EDIT: Copy pasting the Options/Request headers/Response headers of this link
scheme | https |
---|---|
host | mercadoservices.ren.pt |
filename | /api/Exports/GetExports |
language | EN |
nome | PrecoMerc |
dia1 | 2022-08-27 |
dia2 | 2022-08-27 |
dia3 | |
dia4 | |
ordem | |
tipo | |
Address | 185.165.107.11:443 |
Status
200
OK
VersionHTTP/1.1
Transferred618 B (0 B size)
Referrer Policystrict-origin-when-cross-origin
Access-Control-Allow-Credentials | true |
Access-Control-Allow-Headers | Origin, Content-Type, X-Auth-Token, X-ApiKey |
---|---|
Access-Control-Allow-Methods | GET, POST, PATCH, PUT, DELETE, OPTIONS |
Access-Control-Allow-Origin | https://mercado.ren.pt |
Allow | OPTIONS, TRACE, GET, HEAD, POST |
Content-Length | 0 |
Content-Security-Policy | default-src ‘none’ |
Date | Fri, 26 Aug 2022 12:31:56 GMT |
Public | OPTIONS, TRACE, GET, HEAD, POST |
Strict-Transport-Security | max-age=31536000 |
X-Content-Type-Options | nosniff |
X-Content-Type-Options | nosniff |
X-MS-InvokeApp | 1; RequireReadOnly |
X-Powered-By | ARR/3.0 |
X-XSS-Protection | 1;mode=block |
Accept | / |
Accept-Encoding | gzip, deflate, br |
---|---|
Accept-Language | en-US,en;q=0.5 |
Access-Control-Request-Headers | content-type,x-apikey |
Access-Control-Request-Method | GET |
Connection | keep-alive |
DNT | 1 |
Host | mercadoservices.ren.pt |
Origin | https://mercado.ren.pt |
Referer | https://mercado.ren.pt/ |
Sec-Fetch-Dest | empty |
Sec-Fetch-Mode | cors |
Sec-Fetch-Site | same-site |
User-Agent | Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:104.0) Gecko/20100101 Firefox/104.0 |