Reputation: 17066
I am using Scala dispatch HTTP library, version 0.10.1. I make a request to a URL that returns an HTTP 301, permanent redirect. For example, http://wikipedia.com returns a 301 that redirects to http://www.wikipedia.org/. How do I do I use dispatch to get the redirected URL?
Following the tutorial, here's what I've done.
import dispatch._, Defaults._
val svc = url("http://wikipedia.com")
val r = Http(svc OK as.String)
r()
This throws a "Unexpected response status: 301" exception. Presumably I need to either query the r
value for the redirected URL, or maybe specify some argument other than OK
in its definition, but I can't figure out what to do from the documentation.
Upvotes: 8
Views: 2599
Reputation: 85
dispatch / reboot v1.2.0:
val myRequest = url(theUrl)
def myGet = myRequest.GET
def myGetFollowRedi = myGet.setFollowRedirects(true)
def myRequestWithHeader = myGetFollowRedi <:< Map(
"User-Agent" -> "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.",
"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.",
"Accept-Language" -> "de-DE,de;q=0.9,en-US;q=0.7,en;q=0.6,en-GB;q=0.4,nl-NL;q=0.3,nl;q=0.1",
"Accept-Encoding" -> "gzip, deflate, br",
"DNT" -> "1",
"Connection" -> "keep-alive",
"Upgrade-Insecure-Requests" -> "1",
"Sec-Fetch-Dest" -> "document",
"Sec-Fetch-Mode" -> "navigate",
"Sec-Fetch-Site" -> "none",
"Sec-Fetch-User" -> "?1"
)
Upvotes: 0
Reputation: 483
Configure the underlying asyncClient to follow redirects:
val r = Http.configure(_ setFollowRedirects true)(svc OK as.String)
To get the redirected URL:
val svc = url("http://wikipedia.com/")
val r = Http(svc > (x => x))
val res = r()
println(res.getHeader("Location"))
Upvotes: 11