JetClient / jet-client-support

JetClient is an advanced REST API Client plugin for JetBrains IDEs
https://plugins.jetbrains.com/plugin/21173-jetclient
110 stars 0 forks source link

Debug Pre-request scripts #58

Closed tobiashochguertel closed 7 months ago

tobiashochguertel commented 8 months ago

Is your feature request related to a problem?

I have the following pre-request script:

var server = jc.variables.get("keycloak"); // add your Keycloak-URL here (without /auth)
var realm = "Quiz"; // the name of the realm
var grantType = "password"; // the granttype, with password you can login as a normal user
var clientId = "quiz-client"; // the name of the client you created in Keycloak
var username = jc.variables.get("username"); // the username of the user you want to test with
var password = jc.variables.get("password"); // the password of the user you want to test with

// creating the request URL
var url = `${server}/realms/${realm}/protocol/openid-connect/token`;

// creating the body of the request
var data = `grant_type=${grantType}&client_id=${clientId}&username=${username}&password=${password}`;

/*jc.sendRequest({
    url: url,
    method: 'POST',
    header: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: {
        mode: 'raw',
        raw: data
    }
}, function (err, response) {
    // Set the environment variable for token
    var response_json = response.json();
    var token = response_json.access_token;
    jc.variables.set('token', token);
    // You can open up the console in Postman with Alt + Ctrl + C
    console.log('token: ', token);
});*/

jc.sendRequest(new HttpRequest()
    .setUrl(url)
    .setMethod("POST")
    .setHeader("Content-Type", "application/x-www-form-urlencoded")
    .setBodyFormUrlEncoded({ "raw": data })
).then((response) => {
    console.log(response.json())

    // Set the environment variable for token
    var response_json = response.json();
    var token = response_json.access_token;
    jc.variables.set('token', token);
    // You can open up the console in Postman with Alt + Ctrl + C
    console.log('token: ', token);
})

And I don't know why it doesn't work and what of an issue it encounters. I tried to convert it from Postman to Jetclient, but without a console window to see error or debug messages I'm lost.

Describe the solution you'd like

At least a log file where I can see the output of the JavaScript pre-request script when it runs.

Additional context

No response

AntonShuvaev commented 8 months ago

Thank you for your feedback. Adding logging for scripts, as well as logging all requests and responses, is on my roadmap. For now, please use console.log for step-by-step debugging.

tobiashochguertel commented 8 months ago

But where Do I see the console.log output?

AntonShuvaev commented 8 months ago

You can find the console.log output in the Test results tab of the response panel. However, there's a known issue where if an error is thrown in the Pre-request script, the response panel won't display, and only the error message will be shown. I'm planning to address this in future updates.

AntonShuvaev commented 7 months ago

Version 2024.1.6 now includes a console in the Services tool window for viewing requests, responses, and script output. You can also view console.log output from pre-request scripts, even if an error is thrown. Additionally, error messages have been enhanced.