Using the following code to set the body for a HTT...
# kotlin-native
n
Using the following code to set the body for a HTTP POST request:
Copy code
curl_easy_setopt(curlPost, CURLOPT_POSTFIELDS, createPostBody(body).cstr)
Is this the correct way to do it? With the verbose option for libcurl enabled the library prints out 6 for the content length (based on query=up), however the Prometheus server reports that the HTTP request body is missing.
Looks like I have stumbled on a Kotlin Native bug. According to libcurl the POST body is set but the content length is 6. The content length should be 8 since a null terminated String is being passed through as the HTTP body.
No wonder I was going around in circles. The POST was set all along but with the wrong value, and libcurl doesn't have an option to print out the HTTP body so it is a mystery what the value is.
Found out that a CPointer needs to be passed through, instead of a String, eg:
Copy code
curl_easy_setopt(curlPost, CURLOPT_POSTFIELDS, createPostBody(body).cstr.ptr)
After the correction was made Prometheus included the metric in the HTTP response body:
Copy code
{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "__name__": "up",
          "instance": "xxxx:xxxx",
          "job": "prometheus"
        },
        "value": [
          1583706055.41,
          "1"
        ]
      },
      {
        "metric": {
          "__name__": "up",
          "instance": "xxxx:xxxx",
          "job": "push_gateway"
        },
        "value": [
          1583706055.41,
          "1"
        ]
      },
      {
        "metric": {
          "__name__": "up",
          "instance": "xxxx:xxxx",
          "job": "node"
        },
        "value": [
          1583706055.41,
          "1"
        ]
      }
    ]
  }
}
Ended up being very confusing since there is a libcurl example where a String literal is being passed through ( https://curl.haxx.se/libcurl/c/http-post.html ), yet with Kotlin Native the same thing can't be done 🙄.
a
Hi, @napperley! As I can see, everything worked fine in the end, right? I read through the example you linked, but it seems like the K/Ns behaviour is fine. To send Kotlin String like that, one should use
CURLOPT_COPYPOSTFIELDS
option. In your case, it had to be allocated natively and be in memory until the perform is done. It seems to be mentioned in the option’s description here(https://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html).
👌 1
👍 1