Created
April 26, 2012 00:01
-
-
Save rogerleite/2494614 to your computer and use it in GitHub Desktop.
HTTP Multpart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################################################ | |
# Criando uma imagem | |
################################################################################################################ | |
imagens_entry_point = Restfulie.at(IMAGENS_ENTRY_POINT).get | |
nova_imagem = imagens_entry_point.links.nova_imagem.get | |
nova_imagem.imagem.original = "cid:original" #Identificador da imagem na parte não json da requisição, veja a especificação de multpart para mais detalhes (http://www.faqs.org/rfcs/rfc2387.html) | |
nova_imagem.imagem.marca = "Alexandria" | |
nova_imagem.imagem.titulo = "alexandria logo" | |
nova_imagem.imagem.credito = "Fernanda Tarrasco" | |
nova_imagem.imagem.fonte = "Agência Estado" | |
nova_imagem.imagem.categorias = ["Arquitetura, Casa e Decoração","Arquitetura, Casa e Decoração::Ambiente"] | |
nova_imagem.imagem.tags = ["cinema", "vida"] | |
nova_imagem.imagem.status = "disponivel" | |
# O exemplo será feito em Net::HTTP pois Restfulie não suporta upload de imagem. | |
midia_URI = ::URI.parse(nova_imagem.links.imagens.href) | |
request = Net::HTTP.new(midia_URI.host) | |
## | |
# Sobre o corpo da requisição: | |
# - Especificação multpart: http://www.faqs.org/rfcs/rfc2387.html | |
# | |
# ** ATENÇÃO ** | |
# - Não pode haver espaços entre linhas | |
# - Todo fim de linha deve conter "\r\n" (sem as aspas), com excessão da última linha, aquela que contém o último boundary. | |
# - O exemplo abaixo não apresenta "\n" explicitamente no fim de cada linha por já apresentar quebra de linha, implicitamente um "\n". | |
# | |
# | |
# NÃO PODE NEM TER TABULAÇÃO NO CÓDIGO DO BOUNDARY. | |
body = <<-EOF | |
--the_boundary\r | |
Content-Type: application/json; charset=UTF-8\r | |
Content-Disposition: inline; name="json"\r | |
\r | |
#{nova_imagem.to_json}\r | |
--the_boundary\r | |
Content-Type: image/gif\r | |
Content-Disposition: inline; name="original"; filename="#{ IMAGE }"\r | |
\r | |
#{ File.read(IMAGE) }\r | |
--the_boundary-- | |
EOF | |
response = request.post(midia_URI.path, body, {"Content-Type" => "multipart/related; boundary=\"the_boundary\"; type=\"application/json\"; start=\"json\"", "Cookie" => user_cookie}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment