I’m trying to integrate a Java 21 application with WebClient and the qBittorrent API. However, I’m encountering difficulties. When I perform the integration using curl, it works successfully. The same is true when I use Postman or the OkHttp library. I also attempted to use the RestTemplate, but without success.
I’m running qBittorrent with the following Docker image: qbittorrentofficial/qbittorrent-nox:4.6.5-2. Here’s my Docker Compose configuration:
services:
qbittorrent-nox:
container_name: qbittorrent-nox
environment:
- QBT_EULA=accept
- QBT_VERSION=latest
- QBT_WEBUI_PORT=8081
image: qbittorrentofficial/qbittorrent-nox:4.6.5-2
ports:
- 6881:6881/tcp
- 6881:6881/udp
- 8081:8081/tcp
read_only: true
restart: always
stop_grace_period: 30m
tty: true
volumes:
- /home/passella/.qbittorrent-nox/config:/config
- /mnt/sda/:/mnt/sda/
- /mnt/sdb/:/mnt/sdb/
- /mnt/qbittorrent/:/mnt/qbittorrent/
Currently, I can successfully integrate with the API using curl. For example:
curl --location 'http://localhost:8081/api/v2/torrents/add'
--header 'Cookie: SID=MY-SID'
--form 'savepath="/mnt/sdb/stream/series"'
--form 'urls="https://torcache.net/torrent/3B1A1469C180F447B77021074DBBCCAEF62611E7.torrent"'
I can also successfully integrate using Postman. However, when I make the same call from my Java application, I receive the following response:
<200 OK OK,Fails.,[connection:"keep-alive", content-length:"6", content-security-policy:"default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src 'self' 'unsafe-inline'; object-src 'none'; form-action 'self'; frame-ancestors 'self';", content-type:"text/plain; charset=UTF-8", cross-origin-opener-policy:"same-origin", date:"Fri, 09 Aug 2024 22:29:22 GMT", referrer-policy:"same-origin", x-content-type-options:"nosniff", x-frame-options:"SAMEORIGIN", x-xss-protection:"1; mode=block"]>
Additionally, when I check the container logs, I see this error:
bool Http::RequestParser::parseRequestLine(const QString&) invalid http header: "27"
Http::RequestParser::ParseResult Http::RequestParser::doParse(const QByteArray&) header parsing error
Requisição ruim do HTTP, fechando o soquete. IP: ::ffff:172.18.0.1
Here’s my Java code snippet:
private void processItem(final String cookie, final Map.Entry<Item, Data> item) {
final var webClient = clientBuilder.baseUrl("https://localhost:8081").build();
webClient
.post()
.uri("/api/v2/torrents/add")
.header("Cookie", "SID=%s".formatted(cookie))
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters
.fromMultipartData("savepath", item.getValue().dockerPath())
.with("urls", item.getKey().link())
)
.retrieve()
.toEntity(String.class)
.subscribe(System.out::println);
}
I’m using Java 21, Spring Boot 3.3.2, and Gradle. My dependencies are as follows:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'io.projectreactor:reactor-test'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
If you have any further questions or need additional details, feel free to ask!
I attempted to use Rest Template for the integration, but encountered the same error. When using Postman, it generates two types of Java code: one with the UniRest library and another with the OkHttp library. While UniRest leads to the same problem, the integration succeeds when using the OkHttp library. However, my intention is to use WebClient or Rest Template for the solution. The API is functional, as mentioned in the problem. I can test it using curl:
curl --location 'http://localhost:8081/api/v2/torrents/add'
--header 'Cookie: SID=MY-SID'
--form 'savepath="/mnt/sdb/stream/series/"'
--form 'urls="https://torcache.net/torrent/3B1A1469C180F447B77021074DBBCCAEF62611E7.torrent"'
But the idea is to achieve the integration using the Java solution.