Cookies not supported?
In Jsoup I was doing this to login to a website and store the sessionId.
val sessionId = Jsoup.connect(baseUrl)
.data("password", password)
.method(Connection.Method.POST)
.execute()
.cookie(COOKIE_SESSION_ID)
val document = Jsoup.connect(baseUrl)
.cookie(COOKIE_SESSION_ID, sessionId)
.get()
I don't think it's currently possible to do this with Ksoup.
I cannot use install(HttpCookies) for the Ktor client, as we can't pass our Ktor client to Ksoup.
When doing this instead
Ksoup.parseGetRequest(
url = baseUrl,
httpRequestBuilder = { cookie(COOKIE_SESSION_ID, sessionId) },
)
I don't actually see the cookie added to the request headers, so maybe something is up.
Hi @acolombo11 currently, Ksoup does not support cookies or allow passing a custom HTTP client directly. However, you can use Ktor’s HTTP client to fetch the page and then parse the response using Ksoup.
Here’s an example:
val httpResponse: HttpResponse = yourKtorClient.get(url)
val doc = Ksoup.parseInput(input = httpResponse.asInputStream(), baseUri = url)
Note:
httpResponse.asInputStream()is an extension function provided bycom.fleeksoft.ksoup:ksoup-networkfor Ktor 3.x.
If you're using Ktor 2.x, usecom.fleeksoft.ksoup:ksoup-network-ktor2instead.
Hi @acolombo11 , starting from version 0.2.4, you can provide your own HttpClient for network requests.