是否可以像多部分请求一样提供HTTP多部分响应(使用Perl /
PHP)?场景就像是,我想提供一个URL,它为EmployeeID提供一个参数,作为回报,响应应该包括员工的照片,最新的工资单以及姓名,年龄和地址等信息.接收端不是浏览器,但它将是一个获得此响应并稍后处理它们的程序.有关如何做到这一点的任何想法?
为了提供有关上述问题的更多信息,我必须向我的朋友提供一个URL,以便以编程方式接收回复.对于例如(在Perl中):
$response = $ua->request($my_url)
我的应用程序应该不仅响应数据,还响应文件!我被要求让它返回多部分回复.
我现在不知道如何从编码本身开始.谷歌搜索“多部分响应”没有返回我可以学习和开始的页面.然而,chansen的评论非常有用,以及他的代码示例https://gist.github.com/1391017.非常感谢.
解决方法
我正在将我原来的
https://gist.github.com/1391017作为回应.
#!/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--