Julia advice/idioms for interacting with a REST api?

780 views
Skip to first unread message

Duncan Child

unread,
Jun 16, 2014, 1:01:59 PM6/16/14
to julia...@googlegroups.com
Hi all, 

Just getting started with Julia and enjoying it greatly. I have been working on porting some code from iPython notebooks to iJulia. 

I am stumped moving this from Python though:

import requests

In Julia if the payload is not gzipped I found this works:

using Requests
println(JSON.parse(resp.data))

But when it is gzipped it doesn't work (the stack trace is below). 

Some questions are 

1) do you all recommend using 'requests' module when working with JSON payloads and REST apis?

2) does 'requests' have an equivalent method to the Python library's .json() method? 

3) if I have to unzip the payload myself from resp.data how do I do that in Julia? 

Thanks for any help or advice, pointers to a blog post or tutorial would be much appreciated too. 

Duncan 



Unknown value

Line: 0

Around: ... 횏ǑƯ񮖹...

           ^


at In[79]:5

 in error at error.jl:21

 in _error at /Users/duncan/.julia/v0.2/JSON/src/Parser.jl:43

 in parse_value at /Users/duncan/.julia/v0.2/JSON/src/Parser.jl:177

 in parse at /Users/duncan/.julia/v0.2/JSON/src/Parser.jl:236

 in parse at /Users/duncan/.julia/v0.2/JSON/src/JSON.jl:7



Avik Sengupta

unread,
Jun 23, 2014, 7:48:07 AM6/23/14
to julia...@googlegroups.com
There were a few HTTP clients in Julia. Requests is written mostly in pure julia, and is probably the easiest to use. However, as you saw, it doesnt seem to support gzipped responses yet. If you wanted to unzip the responses yourself, take a look at https://github.com/kmsquire/GZip.jl .

One of the other http client libraries is https://github.com/amitmurthy/HTTPClient.jl . This one wraps the cURL libraries, and thus might support gzip out of the box. you may want to try it out as well.

Re: t.json(), Jula is a much more functional language. So the X.Y() idiom is much less prevalant in Julia. In julia, types dont enclose (or contain) methods, unlike most object oriented language.

Regards
-
Avik

Duncan Child

unread,
Jun 25, 2014, 12:41:34 PM6/25/14
to julia...@googlegroups.com
Avik, 

Thanks for the reply. Comments below ... 


On Monday, June 23, 2014 6:48:07 AM UTC-5, Avik Sengupta wrote:
There were a few HTTP clients in Julia. Requests is written mostly in pure julia, and is probably the easiest to use. However, as you saw, it doesnt seem to support gzipped responses yet.

Good to know - thanks for confirming that Requests doesn't support gzipped payloads yet. 
 
If you wanted to unzip the responses yourself, take a look at https://github.com/kmsquire/GZip.jl .

I think that Requests returns the payload as a String which fails for gzipped data. That was when I realized I was yak shaving and posted to the list. 
 

One of the other http client libraries is https://github.com/amitmurthy/HTTPClient.jl . This one wraps the cURL libraries, and thus might support gzip out of the box. you may want to try it out as well.

My workaround was to send uncompressed data from the server. 
 

Re: t.json(), Jula is a much more functional language. So the X.Y() idiom is much less prevalant in Julia. In julia, types dont enclose (or contain) methods, unlike most object oriented language.

Hmmm - it seems like I have lot of Python and Java to unlearn :-) 

Cheers, 

Duncan 

Steven G. Johnson

unread,
Jun 25, 2014, 4:34:50 PM6/25/14
to julia...@googlegroups.com


On Monday, June 16, 2014 1:01:59 PM UTC-4, Duncan Child wrote:
I am stumped moving this from Python though:

import requests

You could always do (after Pkg.add("PyCall"), of course)

using PyCall
@pyimport requests
t = requests.get("......")
t[:json]() 

PyCall is there precisely to make up for any missing functionality on the Julia side that is available in Python modules.
Reply all
Reply to author
Forward
0 new messages