如何获取 POST 请求的正确内容长度

发布于 2024-07-04 10:18:37 字数 883 浏览 8 评论 0原文

我正在使用 perl 脚本发布到 Google Appengine 应用程序。 我使用 -F 选项发布了一个包含一些 XML 的文本文件。

http://www.cpan.org/authors/id/ E/EL/ELIJAH/bget-1.1

有一个版本1.2,已经测试过并遇到同样的问题。 该帖子看起来像这样。

Host: foo.appspot.com
User-Agent: lwp-request/1.38
Content-Type: text/plain
Content-Length: 202

<XML>
   <BLAH>Hello World</BLAH>
</XML>

我已经修改了示例,因此 202 不正确,不用担心。 说到问题。 内容长度与文件上的字节数匹配,但是除非我手动增加内容长度,否则它不会发送所有文件,一些字节会被截断。 对于不同大小的文件,截断的字节数并不相同。 我在脚本上使用了 -r 选项,我可以看到它正在发送的内容,并且正在发送所有文件,但 Google Appengine self.request.body 显示并未收到所有内容。 我认为解决方案是为 Content-Length 获取正确的数字,显然它并不像文件上的字节数或 perl 脚本以某种方式破坏它那么简单。

更新: 感谢埃里克森的正确答案。 我使用 printf 将字符附加到文件末尾,它总是准确地截断文件中的行数。 我想我可以通过迭代服务器端的每个字符来找出要添加的内容,但不值得。 这甚至没有在为应用程序引擎设置的谷歌组中得到回答!

I am using a perl script to POST to Google Appengine application. I post a text file containing some XML using the -F option.

http://www.cpan.org/authors/id/E/EL/ELIJAH/bget-1.1

There is a version 1.2, already tested and get the same issue. The post looks something like this.

Host: foo.appspot.com
User-Agent: lwp-request/1.38
Content-Type: text/plain
Content-Length: 202

<XML>
   <BLAH>Hello World</BLAH>
</XML>

I have modified the example so the 202 isn't right, don't worry about that. On to the problem. The Content-Length matches the number of bytes on the file, however unless I manually increase the Content-Length it does not send all of the file, a few bytes get truncated. The number of bytes truncated is not the same for files of different sizes. I used the -r option on the script and I can see what it is sending and it is sending all of the file, but Google Appengine self.request.body shows that not everything is received. I think the solution is to get the right number for Content-Length and apparently it isn't as simple as number of bytes on the file or the perl script is mangling it somehow.

Update:
Thanks to Erickson for the right answer. I used printf to append characters to the end of the file and it always truncated exactly the number of lines in the file. I suppose I could figure out what is being added by iterating through every character on the server side but not worth it. This wasn't even answered over on the google groups set up for app engine!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜声 2024-07-11 10:18:37

我以前也遇到过类似的问题。

我假设您正在使用 length() 函数来确定文件的大小? 如果是这样,您发布的数据很可能是 UTF-8 编码的,而不是 ASCII。

为了获得正确的计数,您可能需要添加“use bytes;” 将 pragma 添加到脚本顶部,或者将 length 调用包装在一个块中:

my $size;
do {use bytes; $size = length($file_data)}

来自 perlfunc 手册页:

“注意字符:如果 EXPR 采用 Unicode,您将获得字符数,而不是字节数。”

I've run into similar problems before.

I assume you're using the length() function to determine the size of the file? If so, it;s likely the data that you're posting is UTF-8 encoded, instead of ASCII.

To get the correct count you may need to add a "use bytes;" pragma to the top of your script, or wrap the length call in a block:

my $size;
do {use bytes; $size = length($file_data)}

From the perlfunc man page:

"Note the characters: if the EXPR is in Unicode, you will get the number of characters, not the number of bytes."

写下不归期 2024-07-11 10:18:37

您需要的额外字节数是否等于文件中的行数? 我问这个问题是因为也许有可能以某种方式引入了回车符但没有被计算在内。

Is the number of extra bytes you need equal to the number of lines in the file? I ask because perhaps its possible that somehow carriage-returns are being introduced but not counted.

绝不服输 2024-07-11 10:18:37

你如何获得字节数? ..通过查看文件系统上文件的大小?

您可以使用“-s”来获取文件的大小。

或者,如果您想做更多,您可以使用 文件::统计

How are you getting the number of bytes? .. By looking at the size of the file on the filesystem?

You can use "-s" to get the size of the file.

Or, if you want to do more, you may use File::Stat

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文