ksoup icon indicating copy to clipboard operation
ksoup copied to clipboard

Cookies not supported?

Open acolombo11 opened this issue 8 months ago • 1 comments

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.

acolombo11 avatar Apr 16 '25 00:04 acolombo11

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 by com.fleeksoft.ksoup:ksoup-network for Ktor 3.x.
If you're using Ktor 2.x, use com.fleeksoft.ksoup:ksoup-network-ktor2 instead.

itboy87 avatar Apr 16 '25 04:04 itboy87

Hi @acolombo11 , starting from version 0.2.4, you can provide your own HttpClient for network requests.

itboy87 avatar May 28 '25 19:05 itboy87