Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP::Request replacing underscores with hyphens in headers

Tags:

http

header

perl

When specifying custom header containing underscores using HTTP::Request from LWP::UserAgent, the underscores are changed to hyphens:

use LWP::UserAgent;

my $rq = HTTP::Request->new("GET", "http://cpan.org");
$rq->header("X-FOO_BAR", "xyzzy");
print $rq->as_string;

outputs:

GET http://cpan.org
X-FOO-BAR: xyzzy

Is there a way to turn this behavior off?

  • perl v5.26.2
  • LWP:UserAgent 1.9601
  • linux 4.17.8-1-ARCH
like image 655
lash Avatar asked Aug 02 '18 21:08

lash


People also ask

Are underscores allowed in HTTP headers?

Please note that using underscores in headers is perfectly valid per the HTTP spec, but Nginx, by default, will ignore them.

Can HTTP headers have special characters?

The name of the HTTP request header you want to set or remove can only contain: Alphanumeric characters: a - z and A - Z. The following special characters: - and _

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.


1 Answers

Try naming the header with a leading :, as per the documentation in HTTP::Headers under "NON-CANONICALIZED FIELD NAMES":

The header field name spelling is normally canonicalized including the '_' to '-' translation. There are some application where this is not appropriate. Prefixing field names with ':' allow you to force a specific spelling.

like image 154
Jim Davis Avatar answered Sep 23 '22 16:09

Jim Davis