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

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

请先登录再写评论。