Issue
I'm trying to translate a working curl command to groovy, so I can natively execute it in my Jenkins pipeline. My problem is that the curl command works, but my groovy request fails with a 400 error from the pushgateway of prometheus. My code in groovy looks like this:
def post = new URL("https://prometheus_url").openConnection();
def message = '''
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
''';
post.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
post.with {
doOutput = true
requestMethod = 'POST'
outputStream.withWriter { writer ->
writer << message.bytes.encodeBase64().toString()
}
println content.text
}
assert post.responseCode == 200
The error that I'm receiving is
Caught: java.io.IOException: Server returned HTTP response code: 400 for URL: https://prometheus_url
java.io.IOException: Server returned HTTP response code: 400 for URL: https://prometheus_url
The working curl command that I'm trying to copy is the following
cat <<EOF | curl --data-binary @- 'https://prometheus_url' -vvv
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
So my question is, what am I missing between my groovy code and the curl command to be receiving this error?
EDIT: I'm adding the verbose text from the working curl operation
cat <<EOF | curl --data-binary @- 'https://prometheus_url' -vvv
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
* Trying X.X.X.X...
* TCP_NODELAY set
* Connected to prometheus_url (X.X.X.X) port XYZ (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / --------------------
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: CN=--------------------
* start date: Jun 17 00:00:00 2021 GMT
* expire date: Jul 16 23:59:59 2022 GMT
* subjectAltName: host "prometheus_url" matched cert's "----------------"
* issuer: C=--; O=--------; OU=-------- CA 1B; CN=--------
* SSL certificate verify ok.
> POST ------------------------ HTTP/1.1
> Host: prometheus_url
> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
> Accept: */*
> Referer:
> Content-Length: 147
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 147 out of 147 bytes
< HTTP/1.1 200 OK
< Date: Fri, 06 Aug 2021 13:18:39 GMT
< Content-Length: 0
< Connection: keep-alive
<
* Connection #0 to host prometheus_url left intact
* Closing connection 0
Solution
After fiddling further with the code, it seems that the base64 encoding wasn't needed after all. Thus a more simplified and working code is the following:
def post = new URL("https://prometheus_url").openConnection();
def message = """
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
""";
post.setRequestMethod("POST");
post.setDoOutput(true);
post.getOutputStream().write(message.bytes);
assert post.responseCode == 200
Answered By - Evan P Answer Checked By - Pedro (WPSolving Volunteer)