Search code examples
urlencodejavascript-automation

JXA for automation: url encoding for get request


I am trying to send a text via get request to my server, but I have some struggles with spaces/special characters.

How can I encode my text for the request?

(And how to decode it in php?)

Here is my code:

var app = Application.currentApplication();
app.includeStandardAdditions = true;

var text = app.displayDialog("enter your text:", { defaultAnswer: "" }).textReturned;

var result = JSON.parse(app.doShellScript('curl https://example.com?text=' + text));


result

Solution

  • Well, it looks like I found the answer myself:

    encodeURIComponent(text)
    

    seems to be working for me. PHP decoding not necessary.