• This repository has been archived on 18/May/2019
  • Stars
    star
    119
  • Rank 295,956 (Top 6 %)
  • Language
    Julia
  • License
    Other
  • Created about 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[DEPRECATED in favor of https://github.com/JuliaWeb/HTTP.jl] HTTP for Julians

This package is DEPRECATED

We recommend that you use HTTP.jl instead. This package is no longer maintained and has reduced functionality and performance compared to HTTP.jl.

Requests.jl

An HTTP client written in Julia. Uses joyent/http-parser via HttpParser.jl.

Build Status codecov.io

Requests Requests

Quickstart

julia> Pkg.add("Requests")

julia> using Requests
julia> import Requests: get, post, put, delete, options

Make a request

get("http://httpbin.org/get")
post("http://httpbin.org/post")
put("http://httpbin.org/put")
delete("http://httpbin.org/delete")
options("http://httpbin.org/get")

Add query parameters

get("http://httpbin.org/get"; query = Dict("title" => "page1"))
get("http://httpbin.org/get"; query = Dict("multi" => ["value1", "value2"]))

Add plain text data

post("http://httpbin.org/post"; data = "Hello World")

Add JSON data

post("http://httpbin.org/post"; json = Dict("id" => "1fc80620-7fd3-11e3-80a5"))

Add form-encoded data

post("http://httpbin.org/post"; data=Dict(email=>"a", pw=>"b"))

Request compressed data

get("http://httpbin.org/get"; compressed=true)

Send compressed data (GZip)

post("http://httpbin.org/post"; data="compress this data", gzip_data=true)

Set headers and cookies

post("http://httpbin.org/post"; headers = Dict("Date" => "Tue, 15 Nov 1994 08:12:31 GMT"),
                                cookies = Dict("sessionkey" => "abc"))

HTTP basic authentication is parsed and set as a proper Authorization header from the URI:

post("http://username:[email protected]/post")

Set a timeout

This will throw an error if more than 500ms goes by without receiving any new bytes from the server.

get("http://httpbin.org/get"; timeout = .5)    # timeout = Dates.Millisecond(500) will also work

Controls redirects

By default, redirects will be followed. max_redirects and allow_redirects control this behavior.

get("http://httpbin.org/redirect/3"; max_redirects=2)  # Throws an error

# Returns a response redirecting the client to "www.google.com"
get("http://google.com"; allow_redirects=false)  

File upload

The three different ways to upload a file called test.jl (yes this uploads the same file three times).

    filename = "test.jl"
    post("http://httpbin.org/post"; files = [
      FileParam(readall(filename),"text/julia","file1","file1.jl"),
      FileParam(open(filename,"r"),"text/julia","file2","file2.jl",true),
      FileParam(IOBuffer(readall(filename)),"text/julia","file3","file3.jl"),
      ])
    ])

FileParam has the following constructors:

    immutable FileParam
        file::Union{IO,Base.File,AbstractString,Vector{UInt8}}     # The file
        # The content type (default: "", which is interpreted as text/plain serverside)
        ContentType::ASCIIString
        name::ASCIIString                                  # The fieldname (in a form)
        filename::ASCIIString                              # The filename (of the actual file)
        # Whether or not to close the file when the request is done
        close::Bool
    end

    FileParam(str::Union{AbstractString,Vector{UInt8}},ContentType="",name="",filename="")
    FileParam(io::IO,ContentType="",name="",filename="",close::Bool=false)

Inspect responses

Via accessors (preferred):

readstring(::Response)      # Get the payload of the response as a string
readbytes(::Response)       # Get the payload as a byte array
Requests.json(::Response)   # Parse a JSON-encoded response into a Julia object
statuscode(::Response)      # Get the HTTP status code
headers(::Response)         # A dictionary from response header fields to values
cookies(::Response)         # A dictionary from cookie names set by the server to Cookie objects
requestfor(::Response)      # Returns the request that generated the given response
Requests.history(::Response)# Returns the history of redirects that generated the given response.

or directly through the Response type fields:

type Response
    status::Int
    headers::Headers
    cookies::Cookies
    data::Vector{UInt8}
    request::Nullable{Request}
    history::Vector{Response}
end

Saving responses

cat = get("https://upload.wikimedia.org/wikipedia/commons/8/8e/Termografia_kot.jpg")
save(cat, "catpic.jpg")  # Save the payload to a file
view(cat)  # View the payload using your system's default applciation for its mimetype

Streaming API

Write bytes to disk as they are received:

stream = Requests.get_streaming("https://upload.wikimedia.org/wikipedia/commons/9/99/Black_cat_being_snowed_on.jpg")

open("cat.jpg", "w") do file
  while !eof(stream)
    write(file, readavailable(stream))
  end
end

Stream out data of potentially unknown length using chunked encoding:

stream = Requests.post_streaming("http://htpbin.org/post",
  headers=Dict("Transfer-Encoding"=>"chunked"), write_body=false)  
# `write_body=false` causes `post_streaming` to only write the headers, allowing you the chance to write the body manually
for data_chunk in ["first", "second"]
    write_chunked(stream, data_chunk)
end
write_chunked(stream, "")  # Signal that the body is complete

response = readall(stream)  # Get back the server's response

More Repositories

1

HTTP.jl

HTTP for Julia
Julia
630
star
2

Mux.jl

Middleware for Julia
Julia
275
star
3

JuliaWebAPI.jl

Julia package for deploying APIs
Julia
189
star
4

GitHub.jl

A Julia package for interfacing with GitHub
Julia
172
star
5

WebSockets.jl

A WebSockets library for Julia
Julia
157
star
6

Gumbo.jl

Julia wrapper around Google's gumbo C library for parsing HTML
Julia
155
star
7

HttpServer.jl

DEPRECATED! Basic, non-blocking HTTP server in Julia.
Julia
134
star
8

RemoteREPL.jl

Connect a REPL to a remote Julia process
Julia
117
star
9

Hyperscript.jl

Hyperscript: A lightweight DOM representation for Julia
Julia
98
star
10

LibCURL.jl

Julia wrapper for libcurl
Julia
33
star
11

HttpCommon.jl

Types and helper functions for dealing with the HTTP in Julia
Julia
31
star
12

Retry.jl

Macros for simplified exception handling: @repeat try, @retry, @delay_retry, @protected try, @ignore.
Julia
27
star
13

GeoIP.jl

A Julia package to estimate the geographic location of IP addresses
Julia
25
star
14

URIs.jl

URI parsing in Julia
Julia
25
star
15

HTTPClient.jl

DEPRECATED, USE HTTP.jl INSTEAD
Julia
17
star
16

URIParser.jl

Uniform Resource Identifier (URI) parser in Julia
Julia
17
star
17

HttpParser.jl

Deprecated! Julia wrapper for joyent/http-parser
Julia
13
star
18

GitForge.jl

Unified interface for interacting with Git forges
Julia
12
star
19

IPNets.jl

IPv4 / IPv6 network abstractions for Julia
Julia
11
star
20

MIMEs.jl

MIME information: filetype, encoding, gzip
Julia
9
star
21

OpenSSL.jl

Julia
9
star
22

GnuTLS.jl

Transport Level Security for Julia Streams provided by GnuTLS
Julia
8
star
23

UAParser.jl

Parse user-agent strings into components
Julia
7
star
24

Roadmap

Discussion and planning for JuliaWeb packages
7
star
25

LibSSH.jl

A Julia wrapper for libssh.
Julia
6
star
26

URITemplate.jl

RFC6570 URI templates for Julia
Julia
4
star
27

TransportLayerSecurity.jl

TLS abstraction package for Julia
Julia
2
star
28

HTTP2.jl

Julia
1
star
29

LibCURLBuilder

Julia
1
star
30

MbedTLSBuilder

Julia
1
star
31

TLSClient.jl

WIP: Julia interface for OS TLS/TCP (mac/iOS: SecureTransport, Linux: OpenSSL, Win: Schannel...)
1
star
32

GumboBuilder

Deprecated. Now built using https://github.com/JuliaPackaging/Yggdrasil/tree/master/G/Gumbo
Julia
1
star