Jump to content




trying to convert string gotten from HTTP.GET to table



9 replies to this topic

#1 lolmaker2002

  • Members
  • 28 posts
  • LocationSomewhere in Nowhere

Posted 03 March 2017 - 02:54 AM

so, im working on a program to do transaction with Krist, i've figured out how to get the info about the adress, but i cant convert it from a string(which is also a table) to a table. any ideas?

Program:
https://hastebin.com/yolorapeco.lua

--EDIT: thought i might link the API documentation of the Krist currency aswell: http://krist.ceriat.net/docs/

--EDIT2: this is the output i get when i run it:
{"ok":true,"address":{"address":"k5n6r931i1","balance":0,"totalin":612,"totalout":4726,"firstseen":"2016-12-02T21:10:56.000Z"}}

Edited by lolmaker2002, 03 March 2017 - 03:09 AM.


#2 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 03 March 2017 - 03:50 AM

What the Krist API returns is called JSON, it's pretty difficult to parse it yourself and computercraft doesn't offer any built in libraries to handle it. However, ElvishJerricco made a pretty good library to parse it. Here is a link

#3 lolmaker2002

  • Members
  • 28 posts
  • LocationSomewhere in Nowhere

Posted 03 March 2017 - 04:25 AM

seems pretty good! thanks for the help, ill give it a shot.

#4 lolmaker2002

  • Members
  • 28 posts
  • LocationSomewhere in Nowhere

Posted 04 March 2017 - 12:47 AM

UPDATE: IT WORKED! thanks for this, i've been looking for something like this for quite a while :D

#5 lolmaker2002

  • Members
  • 28 posts
  • LocationSomewhere in Nowhere

Posted 04 March 2017 - 02:09 AM

this might not be the right place to post this, but i dont know where to put it elsewhere.

so, i've been trying to make a transaction through computercraft, since im working on a shop, but whenever i try to pass my privatekey through the http.post, it just errors the following:

{"ok":false,"error":"missing_parameter","parameter":"privatekey"}


even tho i posted the key with the http.post
code used to send request:

pay = http.post("http://krist.ceriat.net/transactions?to=k5n6r931i1&privatekey=xxxxxxxx&amount="..payamm.."&metadata=test").readAll()
print(pay) -- checking for errors

if there is anything im doing wrong here please tell me, i've been looking over the internet for more than an hour.

Krist Documentation: http://krist.ceriat.net/docs

Edited by lolmaker2002, 04 March 2017 - 02:10 AM.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 04 March 2017 - 02:35 AM

Threads merged, please keep your questions about the one project in the one place.

It's generally bad practise to post and readAll all in one go. Doing so means you're unable to close the final handle, as you didn't hang on to its pointer anywhere.

In regards to your stated problem, it's possible that you need to make your string URL-safe:

local handle = http.post( textutils.urlEncode( "http://krist.ceriat.net/transactions?to=k5n6r931i1&privatekey=xxxxxxxx&amount="..payamm.."&metadata=test" ) )
local pay = handle.readAll()
handle.close()

print(pay)


#7 lolmaker2002

  • Members
  • 28 posts
  • LocationSomewhere in Nowhere

Posted 04 March 2017 - 02:45 AM

View PostBomb Bloke, on 04 March 2017 - 02:35 AM, said:

Threads merged, please keep your questions about the one project in the one place.

It's generally bad practise to post and readAll all in one go. Doing so means you're unable to close the final handle, as you didn't hang on to its pointer anywhere.

In regards to your stated problem, it's possible that you need to make your string URL-safe:

local handle = http.post( textutils.urlEncode( "http://krist.ceriat.net/transactions?to=k5n6r931i1&privatekey=xxxxxxxx&amount="..payamm.."&metadata=test" ) )
local pay = handle.readAll()
handle.close()

print(pay)

this just returns a nil value index attempt error. it might not be able to find the url? (im quite new to working with the HTTP API)

#8 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 04 March 2017 - 03:29 AM

View Postlolmaker2002, on 04 March 2017 - 02:45 AM, said:

View PostBomb Bloke, on 04 March 2017 - 02:35 AM, said:

Threads merged, please keep your questions about the one project in the one place.

It's generally bad practise to post and readAll all in one go. Doing so means you're unable to close the final handle, as you didn't hang on to its pointer anywhere.

In regards to your stated problem, it's possible that you need to make your string URL-safe:

local handle = http.post( textutils.urlEncode( "http://krist.ceriat.net/transactions?to=k5n6r931i1&privatekey=xxxxxxxx&amount="..payamm.."&metadata=test" ) )
local pay = handle.readAll()
handle.close()

print(pay)

this just returns a nil value index attempt error. it might not be able to find the url? (im quite new to working with the HTTP API)

Yeah you're using the http post method incorrectly, the correct usage is this:
http.post(url, postData)
so in your case
http.post("http://krist.ceriat.net/transactions", "to=k5n6r931i1&privatekey=xxxxxxxxx&amount=" .. payamm .. "&metadata=test")

Edited by Incinirate, 04 March 2017 - 03:29 AM.


#9 lolmaker2002

  • Members
  • 28 posts
  • LocationSomewhere in Nowhere

Posted 04 March 2017 - 03:42 AM

View PostIncinirate, on 04 March 2017 - 03:29 AM, said:

View Postlolmaker2002, on 04 March 2017 - 02:45 AM, said:

View PostBomb Bloke, on 04 March 2017 - 02:35 AM, said:

Threads merged, please keep your questions about the one project in the one place.

It's generally bad practise to post and readAll all in one go. Doing so means you're unable to close the final handle, as you didn't hang on to its pointer anywhere.

In regards to your stated problem, it's possible that you need to make your string URL-safe:

local handle = http.post( textutils.urlEncode( "http://krist.ceriat.net/transactions?to=k5n6r931i1&privatekey=xxxxxxxx&amount="..payamm.."&metadata=test" ) )
local pay = handle.readAll()
handle.close()

print(pay)

this just returns a nil value index attempt error. it might not be able to find the url? (im quite new to working with the HTTP API)

Yeah you're using the http post method incorrectly, the correct usage is this:
http.post(url, postData)
so in your case
http.post("http://krist.ceriat.net/transactions", "to=k5n6r931i1&privatekey=xxxxxxxxx&amount=" .. payamm .. "&metadata=test")
i'll test this tommorrow. its currently 5 in the night

#10 lolmaker2002

  • Members
  • 28 posts
  • LocationSomewhere in Nowhere

Posted 04 March 2017 - 10:11 PM

what Incinirate sais works, but whenever i type the private key of the address, it uses a completely different address to do the transaction, even tho i encoded the url and used the same password as on Lemmmy's web wallet

Edited by lolmaker2002, 04 March 2017 - 10:17 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users