This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
NullSchritt's profile picture

Lua Cutting A String Short? (How To Change Space To %20)

Started by NullSchritt, 09 August 2013 - 02:17 PM
NullSchritt #1
Posted 09 August 2013 - 04:17 PM
Hello all I have written a pretty basic program which automatically dials stargates(a plugin thta provide instantanious treansport between any two locations with a gate) on my server, Currently the program asks the user where they want to go, and then searches my database for an address via a php page.

However it appears that when lua submits the request to my page, it cuts the data off after the first space, causing it to get the wrong address

As you see when you go here: http://projectbuilde...e=nulls%20house

It properly returns the address to my house.


My code is included below, you can run it on any computer in game to see that when you type "nulls house" it will try to dial "nulls TARDIS", because it doesn't include the second word when submitting the request. (http://projectbuilde...gate&name=nulls)

Below is my code:
http://pastebin.com/2YND8rRp (to make it work you need 3 monitors above the pc, and the pc must have a label set)

I think for some reason the input is not being read properly, it seems to stop after the first space.
NullSchritt #2
Posted 09 August 2013 - 04:29 PM
However it seems that I can dial the address if I type in "nulls%20house" and it will work fine.

How should I go about replacing spaces in input string with %20?
MysticT #3
Posted 09 August 2013 - 04:33 PM
You need to encode the url before using it. Look at the url you provided:
http://projectbuilde...e=nulls%20house
note the %20 between nulls and house, that's an encoded space. CC provides a function to do this: textutils.urlEncode(string). Use it on the variables you want to add to the url, like this:

content = http.get("http://smp.projectbuilder.info/fetch.php?cmd=page&ID=dialgate&buffer=0&name=" .. textutils.urlEncode(destination))
NullSchritt #4
Posted 09 August 2013 - 04:51 PM
You need to encode the url before using it. Look at the url you provided:
http://projectbuilde...e=nulls%20house
note the %20 between nulls and house, that's an encoded space. CC provides a function to do this: textutils.urlEncode(string). Use it on the variables you want to add to the url, like this:

content = http.get("http://smp.projectbuilder.info/fetch.php?cmd=page&ID=dialgate&buffer=0&name=" .. textutils.urlEncode(destination))
I implemented this as you stated but it seems seems to produce the same error. Perhaps destination = read() if for some reason not reading the whole string? It seems to only return everything before the first space typed?
MysticT #5
Posted 09 August 2013 - 04:59 PM
Well, I need to see the code to check for other errors, can you upload it again (it looks like you deleted it or it expired)?
NullSchritt #6
Posted 09 August 2013 - 05:05 PM
Nevermind! I was updating the wrong version of my code, it works now, thanks!