I am trying to access the DVLA's Vehicle Enquiry API. Ive got this working with a really simple example in Python, but I really want to get this working in Kotlin using OKHttp so i can use the code in an Android app.
There seems to be some issue i cant figure out with the FormBody.Builder() and the vehicle registration that i put in the body. The error im getting from the API is...
{"errors":[{"status":"400","code":"ENQ108","detail":"Invalid format for field - vehicle registration number","title":"Bad Request"}]}
The DVLA's website shows a cUrl request example, I converted that to Python and it works perfectly, but i cant figure out what im missing when doing it with OKHttp/Kotlin.
You'll see in the Kotlin code i added a println(request) just so i could see what was being sent to the api. that request when printed contains the url + header, but NO body!
Kotlin Code - not working
import okhttp3.*
import java.io.IOException
private val client = OkHttpClient()
private val url_dvla = "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles"
private val auth_dvla = "MY_API_KEY"
fun main(){
run(url_dvla)
}
fun run(url: String){
val requestBody = FormBody.Builder()
.add("registrationNumber", "AP08EKT")
.build()
val request = Request.Builder()
.url(url)
.post(requestBody)
.addHeader("x-api-key", auth_dvla)
.build()
println(request)
client.newCall(request).enqueue(object : Callback{
override fun onFailure(call: Call, e: IOException) {
TODO("Not yet implemented")
}
override fun onResponse(call: Call, response: Response) {
println(response.body?.string())
}
})
}