Proper syntax for sending a HTTP GET request

Is this the correct syntax for sending a HTTP GET request in a JavaScript workflow to send, for example:

http://example.test/api/method?foo=bar

const entities = require('@jetbrains/youtrack-scripting-api/entities');
const http = require('@jetbrains/youtrack-scripting-api/http');
…
exports.rule = entities.Issue.onChange({
…
	action: (ctx) => {
		const connection = new http.Connection('http://example.test/api/method');
    	const queryParams = [{'name': 'foo', 'value': 'bar}];
    	const response = connection.getSync(connection, queryParams);
		if (response && response.code === 200) {
			console.log(response.response);
		}
	}
…
0
4 comments
Official comment

Hi G. K.,

I'm Sergey from the YouTrack Team. Thanks for reaching out!

Your original code passes connection as the first parameter, but getSync expects a URI string. The correct syntax is:

const connection = new http.Connection('http://example.test/api/method');
const queryParams = [{'name': 'foo', 'value': 'bar'}];
const response = connection.getSync('', queryParams);

For self-signed certificates, import the certificate into YouTrack's third-party certificates.

To log response errors, use response.toString():

if (!response.isSuccess) {
    console.warn('Request failed: ' + response.toString());
}

Let me know if you have any questions!

I see an error when I try this:

TypeError: invokeMember (request) on jetbrains.youtrack.scripts.http.ClientWrapper@46d4c641 failed due to: Cannot convert '{url: "http://example.test/api/method", httpClient: JavaOb...'(language: JavaScript, type: Connection) to Java type 'java.lang.String': Invalid or lossy primitive coercion.
0

Okay, my first mistake was passing connection as the first parameter to getSync. Obviously that's not correct. It should be a URI string which is then concatenated with the one passed to http.Connection.

With that fixed, I can access a test endpoint on HTTP.

When I try the real one via HTTPS, however, nothing happens; there is no response, it remained ‘undefined’ and no other indication of error is shown. When I test the HTTPS endpoint using wget, it's necessary to use the --no-check-certificate option since it's a self-signed certificate.

How can I see if that's the problem here, that the HTTPS connection cannot be established?

What's the right way to handle this in YouTrack's http? Is that what the sslKeyName parameter to Connection is for? If so, what's supposed to go in there, the certificate serial number or something else?

0

It does appear the SSL certificate is the problem, though it's odd how I had to learn of this.

console.log(response) gives:

Failed to execute the request. Exception is (certificate_unknown) Non of the delegates checked the chain successfully'

console.log(response.exception) should return an object according to your docs, but returns:

undefined

Why?

How do I ignore the certificate in this case?

0

Please sign in to leave a comment.