mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-09-01 00:00:35 -04:00
added content-type filtering to decoders
This commit is contained in:
parent
c7f177adbb
commit
f520785572
@ -58,9 +58,16 @@ module HTTPX
|
||||
end
|
||||
|
||||
def decode(response)
|
||||
return Transcoder::Form.decode(response) if response.headers["content-type"] == "application/x-www-form-urlencoded"
|
||||
content_type = response.content_type.mime_type
|
||||
|
||||
Decoder.new(response)
|
||||
case content_type
|
||||
when "application/x-www-form-urlencoded"
|
||||
Transcoder::Form.decode(response)
|
||||
when "multipart/form-data"
|
||||
Decoder.new(response)
|
||||
else
|
||||
raise Error, "invalid form mime type (#{content_type})"
|
||||
end
|
||||
end
|
||||
|
||||
def multipart?(data)
|
||||
|
@ -47,7 +47,11 @@ module HTTPX::Transcoder
|
||||
Encoder.new(form)
|
||||
end
|
||||
|
||||
def decode(_response)
|
||||
def decode(response)
|
||||
content_type = response.content_type.mime_type
|
||||
|
||||
raise Error, "invalid form mime type (#{content_type})" unless content_type == "application/x-www-form-urlencoded"
|
||||
|
||||
Decoder
|
||||
end
|
||||
end
|
||||
|
@ -5,6 +5,10 @@ require "json"
|
||||
|
||||
module HTTPX::Transcoder
|
||||
module JSON
|
||||
JSON_REGEX = %r{\bapplication/(?:vnd\.api\+)?json\b}i.freeze
|
||||
|
||||
using HTTPX::RegexpExtensions unless Regexp.method_defined?(:match?)
|
||||
|
||||
module_function
|
||||
|
||||
class Encoder
|
||||
@ -28,7 +32,11 @@ module HTTPX::Transcoder
|
||||
Encoder.new(json)
|
||||
end
|
||||
|
||||
def decode(_response)
|
||||
def decode(response)
|
||||
content_type = response.content_type.mime_type
|
||||
|
||||
raise Error, "invalid json mime type (#{content_type})" unless JSON_REGEX.match?(content_type)
|
||||
|
||||
::JSON.method(:parse)
|
||||
end
|
||||
end
|
||||
|
@ -228,7 +228,7 @@ module Requests
|
||||
HTTPX::Request.new(:get, "http://example.com"),
|
||||
200,
|
||||
"2.0",
|
||||
{ "content-type" => "Content-Type: multipart/form-data; boundary=90" }
|
||||
{ "content-type" => "multipart/form-data; boundary=90" }
|
||||
)
|
||||
form_response << [
|
||||
"--90\r\n",
|
||||
|
Loading…
x
Reference in New Issue
Block a user