简体   繁体   English

Perl HTTP请求content()问题

[英]Perl HTTP Request content() issue

I want to put a file (file.img) as a POST data content, so if the file is in the same folder as the script, is $req->content(file.img); 我想将文件(file.img)作为POST数据内容,因此如果该文件与脚本位于同一文件夹中,则为$req->content(file.img); valid ? 有效吗?

Cheers 干杯

This seems to be something that you could easily test for yourself rather than asking us. 您似乎可以轻松地对自己进行测试,而不用问我们。 What happened when you tried it? 尝试时发生了什么?

(I'm going to assume that $req is an HTTP::Request object - I don't know why you wouldn't include that information in your question). (我将假设$req是一个HTTP :: Request对象-我不知道为什么您不将这些信息包含在您的问题中)。

In short, no, it's not valid. 简而言之,不,这是无效的。 And it's not valid for a number of reasons. 由于许多原因,它是无效的。

It's not valid because file.img would be interpreted as the bareword file concatenated with the bareword img - which would give the string fileimg . 这是无效的,因为file.img将被解释为与裸字img串联的裸字file -这将产生字符串fileimg

It's not valid because use strict makes barewords illegal, so if you're using use strict (and you really should be) your code won't even compile. 这是无效的,因为use strict会使裸词成为非法,因此,如果您使用use strict (实际上应该是),则您的代码甚至都不会编译。

It's not valid because HTTP::Request::content expects a sequence of bytes. 这是无效的,因为HTTP::Request::content需要一个字节序列。 So even if you didn't use use strict all this code would do would be to add the string fileimg to the request body. 因此,即使您不使用use strict所有这些代码也会将字符串fileimg添加到请求正文中。

If you want to add the contents of the file to the body, then you need to open the file, read in the data and then pass that data to content . 如果要将文件的内容添加到正文,则需要打开文件,读入数据,然后将该数据传递给content

This is all explained in the documentation for HTTP::Request . 所有这些都在HTTP :: Request的文档中进行了说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM