Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Additional support for GZipped data #138

Merged
merged 1 commit into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ post("http://httpbin.org/post"; json = Dict("id" => "1fc80620-7fd3-11e3-80a5"))
post("http://httpbin.org/post"; data=Dict(email=>"a", pw=>"b"))
```

### Request compressed data

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

### Send compressed data (GZip)

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

### Set headers and cookies

```julia
Expand Down
12 changes: 11 additions & 1 deletion src/Requests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ function do_stream_request(uri::URI, verb; headers = Dict{AbstractString, Abstra
history = Response[],
tls_conf = TLS_VERIFY,
write_body = true,
gzip_data = false,
compressed = false,
proxy = SETTINGS.http_proxy,
https_proxy = SETTINGS.https_proxy
)
Expand All @@ -339,6 +341,14 @@ function do_stream_request(uri::URI, verb; headers = Dict{AbstractString, Abstra
if "Content-Type" ∉ keys(headers)
headers["Content-Type"] = default_content_type
end
if gzip_data
headers["Content-Encoding"] = "gzip"
body = Vector{UInt8}(body) |> ZlibDeflateInputStream |> read
end
end

if compressed
headers["Accept-Encoding"] = "gzip, deflate"
end

if cookies ≠ nothing
Expand Down Expand Up @@ -389,7 +399,7 @@ function do_stream_request(uri::URI, verb; headers = Dict{AbstractString, Abstra
return do_stream_request(get(redirect_uri), verb; headers=headers,
data=data, json=json, files=files, timeout=timeout,
allow_redirects=allow_redirects, max_redirects=max_redirects,
history=history, tls_conf=tls_conf)
history=history, tls_conf=tls_conf, compressed=compressed)
end
end

Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Compat
using Requests
using JSON
using Base.Test
using Libz

import Requests: get, post, put, delete, options, bytes, text, json, history

Expand Down Expand Up @@ -155,6 +156,12 @@ data = json(res)
# Test for gzipped responses
@test json(get("http://httpbin.org/gzip"))["gzipped"] == true
@test json(get("http://httpbin.org/deflate"))["deflated"] == true
@test json(get("http://httpbin.org/headers"; compressed=true))["headers"]["Accept-Encoding"] == "gzip, deflate"

# Test for gzipped requests
foo_result = "data:application/octet-stream;base64," * (Vector{UInt8}("foo") |> ZlibDeflateInputStream |> read |> base64encode)
@test json(post("http://httpbin.org/post"; data="foo", gzip_data=true))["data"] == foo_result
@test json(post("http://httpbin.org/post"; data=Vector{UInt8}("foo"), gzip_data=true))["data"] == foo_result

# Test timeout delay
let
Expand Down