Search code examples
kotlinintellij-idea

Kotlin HttpClient and HttpRequest unresolved reference errors on Intelij-idea


I can't get references to HttpClient and HttpRequest to import/map properly in my Kotlin code. I'm new to Kotlin, so I might not being configuring the project correctly. I tried this both on Mac and Windows machines, and ran into the same issue.

My Import statements:

import io.jsonwebtoken.Header
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.security.Key
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.*
import kotlin.collections.HashMap

Lines causing issues in inside the main fuction:

val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder()
    .uri(URI.create("https://openapi.example.com/example/"))
    .header("Authorization", "Bearer $jwt")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();

I receive unresolved reference errors:

enter image description here

Configuration:

  • Intelij-IDEA with Kotlin (latest)
  • Amazon Corretto 17
  • Gradle 8.0.1

Solution

  • The import java.net.http.HttpClient was added in JDK 11 but your Gradle configuration should still uses a lower language level.

    Try to open your build.gradle/build.gradle.kts, and change your jvmToolchain to 17. Reload the Gradle in IDEA.