Log In  


Has anyone figured out how to make a http GET request in Picotron? I saw it listed in the roadmap for the 3/14 release but couldn't find a function in the list provided by NuSan here.

Thanks! :D

4


2

Try looking into fetch

Digging into SLATE i found this

local help = fetch("https://raw.githubusercontent.com/scrapSavage/SLATE/main/help.txt")
store("help.txt",help,{})

1

Ah yes, SLATE uses fetch which returns a raw result of whatever http address you put it (fetch is also used to load files). I'm using a Github Raw link so that it only returns the source code I want to download but you can also get the html of websites (eg: print(fetch("https://scrapsavage.dev/"))


Excellent! Thank you both very much!!


Note that there's a couple bugs with fetch:

  1. I can reliably crash Picotron 0.1.0b when calling fetch() containing a remote URL with a query string that is 60+ characters long:
-- Works
print(fetch("https://example.com?ThisQueryIs59CharactersLongThisQueryIs59CharactersLongThisQ"))

-- Crashes Picotron
print(fetch("https://example.com?ThisQueryIs60CharactersLongThisQueryIs60CharactersLongThisQu"))

But, if there's a / immediately before the ?, it works again:

-- Works now with the /
print(fetch("https://example.com/?ThisQueryIs60CharactersLongThisQueryIs60CharactersLongThisQu"))
  1. fetch() forces remote URLs to lowercase / is case insensitive, which breaks requests to some sites (like Fandom; its URLs are case sensitive)

Can confirm it yourself with this testing API that just echos back the requested URL:

print(fetch("https://echo.free.beeceptor.com/UPPER_case"))

@scrapSavage are you having any trouble with fetching remote webpages in newer versions of Picotron? for the last 2-3 versions, it doesn't seem to work for me. If I try and fetch and print any of the example listed in this thread or any other website, I get no output..


1

You need to wrap remote fetches in coroutines in the past few versions, this helps fetches not fail for people with slow internet.


Got it! Thanks so much for the help!


example how coroutine fetch?


@alphaqueueop this is probably unnecessarily convoluted but here's what I've got:

web = {
	requested=nil,
	source="",
	GET=function(self, site)
		self.requested = false
		self.site=site
		if self.requested == false then
			self.cor=cocreate(function (site)
			  	s=fetch(self.site)
				self.source=s
				yield()	
			end)
			self.requested=true
	  	end
	end,
	process_threads=function(self)
		if self.cor and costatus(self.cor) != 'dead' then
			coresume(self.cor,self.site)
		else
			self.cor=nil
		end
	end,
	view_source=function(self)
		print(self.source)
	end
}

function _update()
	if btnp(5) then
		web:GET("https://www.example.com")
	end
	web:process_threads()
end

function _draw()
 	cls()
 	web:view_source()
end

That will pull the source code from a webpage (in this case, example.com) when you press x and print it to the console.



[Please log in to post a comment]