Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ov2015
Visitor

How to get json data from a REST API call?

Hi

I have been trying to gather some data from a different server for which I can use REST API to get datas.
I am trying to use roURLTransfer and its events but have no success yet and always getting

BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty:


My code is as below:

request=CreateObject("roUrlTransfer")
request.SetURL("https://apis.someserver.com/glist")
request.AddHeader("Authorization", "Basic")
request.AddHeader("app_key","username")
request.AddHeader("app_secret","password")
response = ParseJson(request.GetToString())


Can anyone point out what am I doing wrong and what is the right way to connect to some server with given api-keys.

Thanks
0 Kudos
15 REPLIES 15
belltown
Roku Guru

Re: How to get json data from a REST API call?

If the server is using TLS/SSL, then you need to call: request.SetCertificatesFile ("common:/certs/ca-bundle.crt")

Also, don't pass request.GetToString() into ParseJson(). Check the HTTP error code first.
0 Kudos
hugetv
Visitor

Re: How to get json data from a REST API call?

function AuthSerial()

serial = GetDeviceESN()
request = CreateObject("roUrlTransfer")
ba = CreateObject("roByteArray")
ba.FromAsciiString(serial)
request.SetCertificatesFile("pkg:/certs/certs.crt")
request.InitClientCertificates()
request.AddHeader("X-Content", ba.ToBase64String())
request.SetUrl("https://apis.someserver.com/dato.php")
html = request.GetToString()
data = html.Trim()
json = ParseJSON(data)
return json

end function

dato= AuthSerial()

user = dato.user
pass = dato.pass



BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty
Our system http://www.rokumanager.com
0 Kudos
dreamer2057
Channel Surfer

Re: How to get json data from a REST API call?

You need to debug your code to view more details about errors. It might be some https issues with security check.

Take a look: http://forums.roku.com/viewtopic.php?f=34&t=92989
Sincerely, Sergey Shoshin, software developer.
0 Kudos
hugetv
Visitor

Re: How to get json data from a REST API call?

"dreamer2057" wrote:
You need to debug your code to view more details about errors. It might be some https issues with security check.

Take a look: http://forums.roku.com/viewtopic.php?f=34&t=92989


and try everything
Our system http://www.rokumanager.com
0 Kudos
dreamer2057
Channel Surfer

Re: How to get json data from a REST API call?

"belltown" wrote:
Another thing that might help you is to call event.GetFailureReason() to get a more descriptive reason for the error.


"dreamer2057" wrote:

dd = event.GetString()
code = event.GetResponseCode()


and 'print' them

For example..
Sincerely, Sergey Shoshin, software developer.
0 Kudos
hugetv
Visitor

Re: How to get json data from a REST API call?

"dreamer2057" wrote:
"belltown" wrote:
Another thing that might help you is to call event.GetFailureReason() to get a more descriptive reason for the error.


"dreamer2057" wrote:

dd = event.GetString()
code = event.GetResponseCode()


and 'print' them

For example..



I do not understand what you mean
Our system http://www.rokumanager.com
0 Kudos
dreamer2057
Channel Surfer

Re: How to get json data from a REST API call?

Something like this:

 

Function TestHttps() as Dynamic
print "HTTPS STARTED"

url = " ... enter url "
timeout = 1500
num_retries = 1

http = CreateObject("roUrlTransfer")
port = CreateObject("roMessagePort")

http.SetUrl(url)
http.SetMessagePort(port)
http.SetRequest("POST")
http.AddHeader("Content-Type", "application/json")
http.AddHeader("X-Roku-Reserved-Dev-Id")
http.SetCertificatesFile("pkg:/source/certificates/cafile.pem")
http.EnableHostVerification(true)
http.EnablePeerVerification(true)
http.InitClientCertificates()
http.RetainBodyOnError(true)

while num_retries > 0
if (http.AsyncPostFromString(""))
event = wait(timeout, http.GetPort())
print event
if type(event) = "roUrlEvent"
dd = event.GetString()
code = event.GetResponseCode()
print code
if code = 200
print "HTTPS data:" + dd
print "HTTPS code:" + ToString(code)
return dd
else
print "HTTPS data:" + dd
print "HTTPS code:" + ToString(code)
timeout = timeout * 2
num_retries = num_retries - 1
print "HTTPS New try: " + ToString(num_retries)
endif
else
timeout = timeout * 2
num_retries = num_retries - 1
print "HTTPS New try: " + ToString(num_retries)
endif
endif
end while
print "HTTPS FINISHED"
End Function



http.SetCertificatesFile("pkg:/source/certificates/cafile.pem")

It needed if you have ca certificate file that makes domain trusted from untrusted, otherwise
http.SetCertificatesFile ("common:/certs/ca-bundle.crt")


If all will be good: code will be 200 and data will be in correct format (valid json), then need continue to thinking
Sincerely, Sergey Shoshin, software developer.
0 Kudos
hugetv
Visitor

Re: How to get json data from a REST API call?

and how to work with my server certificate
Our system http://www.rokumanager.com
0 Kudos
ov2015
Visitor

Re: How to get json data from a REST API call?

Hi Guys

I am still having the same problem

-77
error setting certificate verify locations:
CAfile: /tmp/plugin/BEAAAA8IJGIU/pkg:/certs/cacert.crt
CApath: none


Also checked this forum link [http://forums.roku.com/viewtopic.php?f=34&t=39683].
May be certificate file is the issue. Where would I get the CA certs file from. I have a cacert.pem file from the ROKU SDK and used openssl to convert it to cacert.crt and used this in the code as well. But still getting the error.

Below is my code, which I have been currently testing by placing it in the Main function. I am trying to get the list of items from the server using the provided server API with its username and password. Any help would be much appreciated.


http = CreateObject("roUrlTransfer")
port = CreateObject("roMessagePort")
http.SetUrl("https://someserver/playlist")
http.SetMessagePort(port)
ba = CreateObject("roByteArray")
ba.FromAsciiString("username:password")
http.AddHeader("Authorization", "Basic " + ba.ToBase64String())
http.AddHeader("Content-Type", "application/json")
'http.AddHeader("X-Roku-Reserved-Dev-Id", "")
http.SetCertificatesFile("pkg:/certs/cacert.crt")
http.InitClientCertificates()
http.RetainBodyOnError(true)

num_tries = 1
while num_tries > 0
if (http.AsyncGetToString()) 'AsyncPostFromString("")
event = wait(0, http.GetPort())
if type(event) = "roUrlEvent"
dd = event.GetString()
code = event.GetResponseCode()
st = event.GetFailureReason()
print code; "--------"
print st; "&&&&&&&&&&&"
num_tries = num_tries -1
end if
end if
end while
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.