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

[SOLVED] http.post(url, postData) return nil where http.post(url) returns a handle

Started by Codinget, 29 July 2017 - 02:22 PM
Codinget #1
Posted 29 July 2017 - 04:22 PM
Hi,
So I'm not really new to CC (been using it for a few years now) but I just recently started using the HTTP api.
What I'm trying to do is a log system (the computer POST's on a URL and the server logs it).

On the server, for testing, the code is just printing back the body of the request along with a form to test it with the browser.

On ComputerCraft, http.get(url) works but http.post(url,postData) doesn't.
But in my browser, it works.
And my server doesn't log any error, nor does it log the POST request from CC…

My code:

local URL="http://raspnathan.familledecher.com:48484/CC/api/report/tree"
local a=http.get(URL)
local b=http.post(URL,"a=1")
print(a)
print(B)/>/>
if a then a.close() end
if b then b.close() end

I get the following result:

table: 23680b25
nil

So I was wondering if I did something wrong and if you guys could help me.
Also sorry if I wasn't descriptive enough.
Edited on 02 August 2017 - 01:37 PM
Bomb Bloke #2
Posted 30 July 2017 - 01:33 AM
Which version of ComputerCraft are we talking about? This sounds related to a bug in one of the recent pre-release builds, although an even more recent one should fix it if that's the case.
Codinget #3
Posted 30 July 2017 - 10:49 AM
I'm using SquidDev's 1.12 patch (ComputerCraft-SquidDev-CC-ComputerCraft-feature-minecraft-1.12.2-1.80pr0-build18.jar), it was said that it only fixed compatibility problems, so I thought it would be okay… When I downloaded this patch a few days ago, it was the latest version available for 1.12
If that's the problem, would trying to patch the official version myself work better? Or could I rebuild only HTTPRequest.java and the like and use them to patch the version I'm using?
Sorry I didn't think about the version not working…
Bomb Bloke #4
Posted 31 July 2017 - 03:24 AM
Not all builds on the source site are documented (and none of them are timestamped!), but I do notice there's a build20 there. It still errors on your code, though if I go back to a much older version of CC I'm able to get the post through.

So I do believe recompiling just the current master version of HTTPRequest.java should sort it. Note that there're two files with that name, though - you want the one in the http folder.
SquidDev #5
Posted 31 July 2017 - 07:50 AM
This is caused by me mis-using the Content-Encoding header, showing I should really read the spec before doing that sort of thing - sorry. I've submitted a pull request to fix it, but cannot say how soon it will be merged. A temporary fix would be to override the header:

local b = http.post(URL, "a=1", { ['content-encoding'] = "identity" })

It's worth noting that when the http methods fail, they may return a handle as the third argument, allowing you to see the response:


local a, b, c = http.post(URL, "a=1")
if not a then
  printError(B)/>
  print(c.readAll())
end

Not all builds on the source site are documented (and none of them are timestamped!).
Well they are, it's rather hard to get to. The section later down does include the commits used to generate each build, which should be equivalent to the timestamp, though I guess it would be possible to include both there. I'm open to any feedback on the CI site - we redesigned it to hopefully be more intuitive to the average user, but goodness knows how successful we were.
Edited on 31 July 2017 - 05:50 AM
Codinget #6
Posted 31 July 2017 - 03:38 PM
Thanks for your answers!
So yeah, @SquidDev's lua fix works, and apparently, editing HTTPRequest.java to remove line 141 seems to work too!
TBH I didn't know this behavior of the HTTP protocol so the source looked good for me :/
Next time, I'll try investigating the source more to find these bugs, and try helping too…