Url encoding string

Is there an easy way to encode a string to make it a valid URL?
I need to send an SQL query using a web api, but int he query there is a field that contain a “free string” so It can contain whatever chars such as à,é," and not only spaces,quotation marks and apex.
How can i transform it to a valid URL?

2 Likes

I was having this question too :question:

It is easy to encode strings. I can do a simple JavaScript function and demonstrate it but the actual question is:
When you encode characters from your side, will the server know this is encoded to decode it before using it.

You need to provide more info because Thunkable has a block for Web API Query which should pass the required text without problems.

1 Like

How to do this , i needed it for a get function(not query parameters)

1 Like

You could easily do that by using btoa() JavaScript function with the use of the Web Viewer Extension

<!DOCTYPE html>
<html>
<head>
<script src="https://thunkable.github.io/webviewer-extension/thunkableWebviewerExtension.js" type="text/javascript"></script>
<script>
    ThunkableWebviewerExtension.receiveMessage(function(message) {
        ThunkableWebviewerExtension.postMessage(btoa(message));
    } );
</script>
</head>
<body>
</body>
</html>

This will accept a string from the Web Viewer PostMessage block, convert it to Base64 string and send it back to Web Viewer ReceiveMessage block to use it in your app.

2 Likes

Thank you @muneer Will try this

Will it work even with thunkableX?

1 Like

Yes, this code is designed for Thunkable X.

However for your SQL request, I need more details of what type of SQL DB are you using and what type of API and why do you want the “query” text to be part of the URL.

The API is made by myself as an interface with an SQL SERVER DB on my local net.
A simple software that receive a query as parameter from an URL.
This is an example:

http://192.222.115.245:8001/SQL_Server_API/SELECT?QUERY=SELECT%20*%20FROM%20MYTABLE%20WHERE%20ID=%27559%27%20AND%20LINEA=%27TR389%27

I have some problem when I have an original string like this to pass to the URL:
WHERE DESCRIZIONE='Controllo conicità e verifica diametro 4" ’

1 Like

In this case you will need to change the accented characters to numbers like %20 for a space. So for á you use %C3%A1 and so on.

You can find the codes by using urlencode.io for example or any similar site.

Of course, this is why I was asking for a simple way like a “url encoding” function.
I know I can do it coding a behavior for every special char, but you know, maybe in a web API a similar function could have been already developed and I didn’t know :wink:

1 Like

API(s) are server side apps created by developers like me and you to serve a certain purpose. The developer may add functionality in the service and may not.

This is why every API is different. One API could use encoding and another would not.

In this case i should be able even to use a browser to get a SELECT result so I need encoding

1 Like

The standard format of an API response is JSON or XML format and both can include characters of different languages.

1 Like

The problem is not the response but the request :slight_smile:

You could easily do that by using btoa() JavaScript function with the use of the Web Viewer Extension

However it’s not clear tome how to use it.
I’ve tried like this

But I got nothing back…

1 Like

As I have explained earlier, you cannot just an encoding algorithm on one side of the equation and not the other. If you encode your URL query then there should be a function in the receiving side to decode it back.

My suggestion is to use the character encoding as I showed in the previous post.

Don’t care about the receiving side.
On that side, the decoding is implicit in the GET function, no need to decode it back.
At the moment my only need is to decode to URL.
But as I understood I need to do it replacing special chars one by one,no automatic function.

@muneer,
as I already wrote in my post “Web app passing parameter URL”, I’d like to call my web app (Thunk) encoding the paramenter and get it inside the app.
In your advice is it possible that?

Thanks
Andrea

1 Like

You can always pass parameters between a Web Viewer and the containing app but I don’t know if a web app will receive parameter from the URL.

The only way to find out is to try it.