Base64-encoding a string in the HTTP client

Hi there,

I'm trying to base64-encode a string using a pre-request script. Is that possible?

Here's what I've attempted to do:

### test query
< {%
  const base64_test = btoa('test-string') ;
    request.variables.set("base64_test",base64_test);
%}
POST ...

However, PHPStorm flags the btoa() function call, saying "Unresolved function or method btoa()". This is a legitimate JavaScript function, according to MDN (https://developer.mozilla.org/en-US/docs/Web/API/btoa), but doesn't seem to be available in PHPStorm. Does PHPStorm have a limited or restricted subset of JavaScript functions? If so, is there documentation describing what functions are and are not available?

Thank you.

7
11 comments

What's wrong with this forum, it's the second time this week I carefully compose an answer just to find it's gone.

I won't repeat it all, but I was suggesting to get the pollyfill in the MDN page into a file and then import it. Lastest PhpStorm versions supports imports.

0

Alvaro, sorry about that. I've seen it a couple of times myself - the reply would be just gone on posting, immediately.
I reported it to our Zendesk team; hopefully, they'll find the root cause soon.

1

Alvaro Thanks for your response. I'll give it a shot!

As for disappearing from data, I feel your pain. And felt it so often over the years that I made it a habit to take screenshots and copy all of the text in every web form I fill out. It has saved my sanity on several occasions. Cheers! (I'm copying this text, too!)

1

What's wrong with this forum, it's the second time this week I carefully compose an answer just to find it's gone.

When this happens, if you click the Back button to go to the previous page, the content should still be there (it is for me). Just click on the Submit button again -- it should post it OK this time.

1

Http client uses es5 not es6

0

You can eval a customBtoa function from your env file (this function only works for ASCII and was written by ChatGPT)

{
  "dev": {
    "customBtoa": "function customBtoa(str) {var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var result = ''; var i = 0; var cur, prev, digitNum; var curBits, prevBits, bitsNumShifted; while (str.length % 3) {str += '='; result += '=';} while (i < str.length) {cur = str.charCodeAt(i); prev = i > 0 ? str.charCodeAt(i - 1) : 0; digitNum = i % 3; curBits = cur.toString(2); prevBits = prev.toString(2); while (curBits.length < 8) curBits = '0' + curBits; while (prevBits.length < 8) prevBits = '0' + prevBits; bitsNumShifted = (digitNum === 0) ? curBits.substr(2) : (digitNum === 1) ? prevBits.substr(6) + curBits.substr(0, 4) : prevBits.substr(4) + curBits.substr(0, 2); result += characters.charAt(parseInt(bitsNumShifted, 2)); i++; } return result;}"
  }
}

Use it in your .http

### Post to node-red /test a json object
< {%
    eval(request.environment.get("customBtoa"));
    const btoa = customBtoa("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
    request.variables.set("hash", btoa);
%}
POST http://localhost:1880/test
Content-Type: application/json

{
    "message": "Hello World!",
    "hash": "{{hash}}"
}
0

This customBtoa from ChatGPT seems to give incorrect strings for inputs like “testing” without any special characters at all. For example:

input:

testing

output from customBtoa:

==0GV0Glnz0

output from https://www.base64encode.org/

dGVzdGluZw==

And even if it handled everything mentioned in var characters correctly, it still couldn't encode JSON since it doesn't support curly braces, quotation marks or colons.

0

I found this thread because I wanted to base64 encode my auth string in PHPStorm HTTP message and btoa() does not work in the jsHandleer. I saw the customBtoa function by Alexandre Boyer and Mika Harju's comment referencing some incorrect strings returned from the function. 

The following seems to work for me in my env file:

http-client.env.json

{
    "testing": {
        "base64EncodeFunction": "function base64Encode(a){for(var e,r,o,t=\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\",c=\"\",n=0;n<a.length;)e=a.charCodeAt(n++),r=a.charCodeAt(n++),o=a.charCodeAt(n++),c+=t[e>>2],c+=t[(3&e)<<4|r>>4],c+=isNaN(r)?\"=\":t[(15&r)<<2|o>>6],c+=isNaN(o)?\"=\":t[63&o];return c} "
    }
}

 

request.http

### Test base64Encode()
< {%
    eval(request.environment.get('base64EncodeFunction'));
    request.variables.set('basicAuth', base64Encode('username:password'))
%}
POST ...
0

I like an approach made by Adam. However, in the latest version of my GoLand IDE `eval()` does not work at all. It fails to run even very basic example like:

### Test eval
< {%
    eval('var x = 42;');
    console.log(x)
%}
POST ...

One gets that runtime error:

ReferenceError: x is not defined
    :module:eval (at 

0

Radek Pysny Hi there! Thank you for your question. Please try something like this:

### Test eval
< {%
    let x;
    eval('x = 42;'); // Assign x inside eval, but it's scoped to the outer declaration
    console.log(x); // Now x is defined within the script scope
%}
POST ...

If I understood everything correctly, in your case, x is defined inside of eval(). Hence, console.log(x) doesn't see it as it's used in another scope. Please let me know what you think.

0

Vadym Veprytskyi Good day! Thank you for contacting me. FIrst of all, I wanted to put here some context:

I wanted to define  a helper method to be used in that code snippet. I know, this is possible, but I wanted to keep function definition in a env file (so it might be shared across multiple request definitions). I hit some older forum posts where users (of HTTP client was) used that approach.

However, I have found after a few hours later, that for my common problem is elegant solution. HTTP client supports base64 encoding via `Window.btoa('id/42')`. So I lost the need of solving that problem for now. Perhaps, later I would find some reason for it, but now I am OK.

And to answer your question, yes this is right. Some time ago, what was run by eval was made available to the environment. I believe that sandboxing technique or some JS engine version bump cancelled that “side effect”, so one might take use of eval anymore.

Once again. Thank you for your reply. Thanks to `Window.btoa` for base64 encoding of IDs for GraphQL API, I do not need it right now.

0

Please sign in to leave a comment.