使用Perl或PHP的HTTP多部分响应 [英] HTTP multipart response using Perl or PHP

查看:105
本文介绍了使用Perl或PHP的HTTP多部分响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像多部分请求一样提供HTTP多部分响应(使用Perl / PHP)?场景就像是,我想提供一个URL,它为EmployeeID提供一个参数,作为回报,响应应该包括员工的照片,最新的工资单以及姓名,年龄和地址等信息。接收端不是浏览器,但它将是一个获得此响应并稍后处理它们的程序。关于如何做到这一点的任何想法?

Is it possible to provide HTTP multipart response (using Perl/PHP) just like multipart request? The scenario is like, I would like to provide a URL whick takes a parameter for EmployeeID, and in return the response should consist of employee's photo, latest payslip and information like name, age and address. The receiving end is not a browser, but it will be a program which gets this response and process them later. Any idea on how to do this?

为了提供有关我上述问题的更多信息,我必须向我的朋友提供一个URL,他将以编程方式接收回复。对于例如(在Perl中):

To give more information on my above question, I have to provide a URL to my friend who will programmatically receive the response. For e.g. (in Perl):

$response = $ua->request($my_url)

我的应用程序不仅应该响应数据,还应该响应文件!我被要求让它返回多部分响应。

My application is supposed to respond with not just data, but also with files! I was being asked to make it to return multipart response.

我现在不知道如何从编码本身开始。谷歌搜索多部分响应没有返回我可以学习和开始的页面。但是,chansen的评论非常有用,以及他的代码示例 https://gist.github.com/1391017 。非常感谢。

I did not now how to start with the coding itself. The google search on "multipart response" did not return pages with which I can learn and get started. However, the comment by chansen was really helpful, as well as his code sample https://gist.github.com/1391017. Thanks a lot.

推荐答案

我正在构建我原来的 https://gist.github.com/1391017 作为回复。

I'm posing my original https://gist.github.com/1391017 as response.

#!/usr/bin/perl
use strict;
use warnings;

use HTTP::Response;

my $response = HTTP::Response->new(
    200, 'OK', [ 'Content-Type' => 'multipart/form-data' ]
);

$response->protocol('HTTP/1.1');
$response->date(time);
$response->server('Foo/1.0');

my $name = HTTP::Message->new([
    'Content-Type'        => 'text/plain; charset=UTF-8',
    'Content-Disposition' => 'form-data; name="name"',
], 'John Doe');

$response->add_part($name);

my $note = HTTP::Message->new([
    'Content-Type'        => 'text/plain; charset=UTF-8',
    'Content-Disposition' => 'form-data; name="note"',
], <<'NOTE');
Resources:
  o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Message.pm
  o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Response.pm
  o http://tools.ietf.org/html/rfc2388
  o http://tools.ietf.org/html/rfc2616
NOTE

$response->add_part($note);

my $blob = HTTP::Message->new([
    'Content-Type'        => 'application/octet-stream',
    'Content-Disposition' => 'form-data; name="blob"; filename="blob.bin"',
]);
$blob->add_content('a chunk');
$blob->add_content(' of data');

$response->add_part($blob);

print $response->as_string;

输出:

HTTP/1.1 200 OK
Date: Thu, 24 Nov 2011 10:03:25 GMT
Server: Foo/1.0
Content-Type: multipart/form-data; boundary=xYzZY

--xYzZY
Content-Type: text/plain; charset=UTF-8
Content-Disposition: form-data; name="name"

John Doe
--xYzZY
Content-Type: text/plain; charset=UTF-8
Content-Disposition: form-data; name="note"

Resources:
  o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Message.pm
  o http://search.cpan.org/dist/HTTP-Message/lib/HTTP/Response.pm
  o http://tools.ietf.org/html/rfc2388
  o http://tools.ietf.org/html/rfc2616

--xYzZY
Content-Type: application/octet-stream
Content-Disposition: form-data; name="blob"; filename="blob.bin"

a chunk of data
--xYzZY--

这篇关于使用Perl或PHP的HTTP多部分响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆