Skip to content

davelooi/http_multipart_parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Multipart Parser

Build Status

Use this gem to parse multipart messages into coherent Ruby-flavoured pieces.

Usage

Imagine your API vendor returns you a multipart body that looks like this:

--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Type: text/xml

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body /></S:Envelope>
--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Id:<[email protected]>
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

"first_name","last_name","preferred_name"
"david", "looi", "dave"

--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50--

But you only care about that CSV data near the bottom. How on earth are you supposed to process this?

Like this!

input = %(
--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Type: text/xml

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body /></S:Envelope>
--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50
Content-Id:<[email protected]>
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

"first_name","last_name","preferred_name"
"david", "looi", "dave"

--uuid:0909325b-5e74-44eb-9a5e-4c7067f14c50--
)

response = HttpMultipartParser::Response.new(input)
puts response.part_by_content_type('application/octet-stream').body

Reference

This gem is built based on this RFC1341(MIME).

About

Parse HTTP multipart response

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published