network-programmingkotlinheaderokhttpmedia-type

Okhttp3 in Kotlin, String.mediaType() does not work


This is the code that I am trying to do but it doesn't work.

 val JSON = String.format("application/json; charset=utf-8").toMediaType()
  (headers as Map<String, String>).toHeaders()

I did import: import okhttp3.MediaType What do I need to do to make it recognise the toMediaType and toHeaders methods?


Solution

  • The extension functions are in companion objects:

    import okhttp3.MediaType.Companion.toMediaType
    import okhttp3.Headers.Companion.toHeaders