Hi, I’m trying to use CUrl <https://github.com/Jet...
# kotlin-native
a
Hi, I’m trying to use CUrl https://github.com/JetBrains/kotlin-native/blob/master/samples/libcurl/src/libcurlMain/kotlin/CUrl.kt for POST data I’ve added SSL
Copy code
curl_easy_setopt(curl, CURLOPT_PORT, 443L)
curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS)
and method POST
Copy code
fun post(postData: String) {
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headersCRef)
    val cstr = postData.cstr
    curl_easy_setopt(curl, CURLOPT_POST, 1)
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cstr)
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, cstr.size)
    val res = curl_easy_perform(curl)
    curl_slist_free_all(headersCRef)
    if (res != CURLE_OK) {
        throw RuntimeException("curl_easy_perform() failed: ${curl_easy_strerror(res)?.toKString()}")
    }
}
But the server accepts wrong data - without some chars (at the start or at the end). What I do wrong? How it can be fixed? FIXED:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cstr)
->
_curl_easy_setopt_(curl, _CURLOPT_POSTFIELDS_, cstr.getPointer(MemScope()))
n
Alternatively you can use CURLOPT_COPYPOSTFIELDS instead of CURLOPT_POSTFIELDS, which will do a copy by value.
a
I’ve tried it, but without
getPointer(MemScope())
it didn’t work 🙂
v
Slack is a goldmine for information... I've no idea what
Copy code
data.cstr.getPointer(MemScope())
actually does, but it does work. Thank you, @aleksey.tomin, I've been trying for hours to get a curl POST to work. And I agree, `CURLOPT_COPYPOSTFIELDS`doesn't work; in fact, it seems to hang the application.
a
Now I use ktor-client-curl. I think it's the best way
v
I tried that but I don't think they have a version of ktor which works on LinuxArm32Hfp for my Raspberry Pi.
👍 2