# Fetch the answer from the Internet

So, normally, this is a forbidden loophole to fetch the desired output from an external source. But this challenge is an exception ! In this challenge you will have to fetch data from the internet and output it to the SDTOUT.

1. For the purpose of this challenge, you'll be the owner of the imaginary a.pi domain!
2. You will need to take the first argument from STDIN, pass it to a.pi/{arg}, and print the result to STDOUT.
3. You don't need to handle errors, the server is always up and running !
4. This is a , the winner is the shortest (in bytes or equivalent) valid answer.

# Rules

1. You are allowed to invoke curl and wget [1] commands as child/sub-processes (like child_process with NodeJS).
2. The ressource at a.pi will only be available in the http and https protocol in their default ports (80 and 443 respectively), you don't need to include the http:// if your language allow to ommit it.
3. Only the desired output (http response) should be printed, nothing else (no GET http://a.pi/: or whatever).
4. The input might contain spaces or otherwise reserved characters, and need to be percent-encoded.
Spaces ( ) should be replaced with %20 and % with %25 (As spaces cannot be in URLs). The other characters (including reserved ones) don't need to be encoded, but you can encode them (including unreserved characters) if that makes your code shorter.
5. If your language/environement doesn't have a STDIN/STDOUT (like in a browser), the shortest equivalent can be used (like prompt() and alert() in javascript).
6. The CORS Headers are open for any external requests (Access-Control-Allow-Origin: *), if you where wondering.

Note: to test your program, you can use any domain and then replace it with a.pi for the submission.

[1] While it should works the same everywhere, I'd recommend you to precise which OS you used for testing.

Rule 4 have been modified as it was the hardest part is the percent-encoding, which is already a submitted code golf, and the exact rules of the encoding where unclear.

Note: this the first code-golf challenge that I submit, and I didn't knew about the sandbox before. In the future, I'll definitely post here before submitting.
Also, this change doesn't affect previous entries (Except for their length).

• Points 1 and 2 are unclear and basically unobservable. curl and wgets are essentially language-specific tools, and what do you mean by "the included library"? I suggest you use the Sandbox to get feedback on challenge ideas before posting them to main Nov 23, 2021 at 13:23
• @cairdcoinheringaahing Thanks for pointing this out! I removed the rule 1 (and shifted the rule numbers) So now you are allowed to use these command-line tools no matter what, as choosing which methot is shorter (like child_process vs http.get()) is part of the challenge. Btw I didn't knew about the Sandbox, I'll use it for next golf challenges that comes to my mind. Nov 23, 2021 at 13:59
• Are spaces the only characters we should percent-encode (%20)? If not, what are all possible inputs (Letters/digits/spaces only? Printable ASCII only?), and which need to be encoded besides spaces? Also, are + allowed to encode spaces specifically? Nov 23, 2021 at 15:11
• Does this answer your question? Percent-Encode a String Nov 23, 2021 at 17:13
• This looks like the main task here is percent-encoding the input string (for which we have a challenge: codegolf.stackexchange.com/questions/89767/…). Nov 23, 2021 at 17:14

# 05AB1E, 29 20 bytes

„ %DÇh'%ì‡"a.pi/"ì.w


-9 bytes now that the encoding rule (4) has changed.

Try it online (where .w will act as no-op, since it's disabled on TIO).

Original 29 bytes answer encoding all reserved and non-ASCII characters:

žj“"+-.<>“«мDÇh'%ì‡"a.pi/"ì.w


$$\\frac{2}{3}rd\$$ of the program is used to encode the characters (19 bytes to be exact).

Try it online (where .w will act as no-op, since it's disabled on TIO).

Explanation:

„ %                # Push string " %"
D               # Duplicate this string
Ç              # Convert it to a list of codepoint integers
h             # Convert each integer to hexadecimal
'%ì         '# Prepend a "%" in front of each
‡         # Transliterate all characters in the (implicit) input-string
"a.pi/"ì # Prepend "a.pi/" in front of it
# read and push its contents
# (after which this is output implicitly as result)

žj                 # Push builtin "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
“"+-.<>“«       "# Append string '"+-.<>'
м       # Remove all these characters from the (implicit) input-string,
# so we're left with all reserved and/or non-ASCII characters
DÇh'%ì‡"a.pi/"ì.w '# Same as above

• I half expected the "bytes" link in "19 bytes" to link to The Nineteenth Byte :P Nov 23, 2021 at 18:17
• Shouldn't that be "⅔ʳᵈˢ"?
– Neil
Nov 24, 2021 at 16:52

# Javascript (ES6), 26 23 bytes

x=>fetch('/'+escape(x))


Percent encode builtin FTW!!!

Returns a Promise containing an object with a text method, which when called with no properties returns the result, allowed by this.

Must be run on the a.pi domain, allowed by this consensus. Since this is impossible, try running the following in your console, on this site:

f=
x=>fetch('/'+escape(x))

f('questions').then(x=>x.text()).then(console.log)


(This requests the questions page)

• Can you use escape instead of encodeURI? Nov 24, 2021 at 11:36
• @DomHastings Yep! Nov 24, 2021 at 18:27

# Perl 5 -nlMv5.10, 50 bytes

s/\W/sprintf"%%%02x",ord$&/ge;saycurl -s a.pi/$_


Can't try it online unless you swap the two  chars with " to output the curl command with it's arguments instead of it's response. Percent-encodes all chars except a-z, A-Z, 0-9 and _.