Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用perl向302响应添加响应头_Perl_Http_Http Headers_Http Status Code 302 - Fatal编程技术网

使用perl向302响应添加响应头

使用perl向302响应添加响应头,perl,http,http-headers,http-status-code-302,Perl,Http,Http Headers,Http Status Code 302,我正在尝试编写一个perl页面,该页面将http 302响应返回到不同的位置,并向该响应添加一个自定义头。 所以我想要的http响应应该是这样的: HTTP/1.1 302 Moved Date: Sun, 15 Apr 2012 10:59:02 GMT Server: Apache Location: http://www.google.com Content-Length: 396 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive

我正在尝试编写一个perl页面,该页面将http 302响应返回到不同的位置,并向该响应添加一个自定义头。 所以我想要的http响应应该是这样的:

HTTP/1.1 302 Moved
Date: Sun, 15 Apr 2012 10:59:02 GMT
Server: Apache
Location: http://www.google.com
Content-Length: 396
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
CUSTOM_HEADER: CUSTOM_VALUE
我尝试使用CGI:

#!/bin/perl

use strict;
use APR::Request::Apache2;
my $r = shift;
$r->content_type('text/html; charset=utf-8');
$r->headers_out()->add("CUSTOM_HEADER", "CUSTOM_VALUE");
$r->headers_out()->add("Location", "http://www.google.com");
$r->status(302);
我收到了谷歌302的回复,但没有自定义的标题。一旦我通过
$r->status(200)将状态更改为200我确实得到了自定义的_头。

那么这种行为是怎么回事?如何将标题添加到302响应?

您应该使用。即使出现错误和重定向,也会打印这些内容。

您应该使用。即使出现错误和重定向,也会打印这些内容。

使用
$r->err\u headers\u out->set
$r->err\u headers\u out->add

my $r = shift;

$r->content_type('text/html; charset=utf-8');
$r->err_headers_out->set(Location => "http://www.google.com");
$r->status(302);

使用
$r->err\u headers\u out->set
$r->err\u headers\u out->add

my $r = shift;

$r->content_type('text/html; charset=utf-8');
$r->err_headers_out->set(Location => "http://www.google.com");
$r->status(302);

谢谢你的帮助。顺便问一下,为什么302会被视为错误响应?@Oded-好问题。我明白你的意思:)状态码1x是信息性的,2xx成功,3xx重定向,4xx客户端错误和5xx服务器错误。所以302不是成功,不是错误。那是什么呢?在这种情况下,此模块和功能是错误的。不要问为什么:)谢谢你的帮助。顺便问一下,为什么302会被视为错误响应?@Oded-好问题。我明白你的意思:)状态码1x是信息性的,2xx成功,3xx重定向,4xx客户端错误和5xx服务器错误。所以302不是成功,不是错误。那是什么呢?在这种情况下,此模块和功能是错误的。不要问为什么:)