如何获取 POST 请求的正确内容长度
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以前也遇到过类似的问题。
我假设您正在使用 length() 函数来确定文件的大小? 如果是这样,您发布的数据很可能是 UTF-8 编码的,而不是 ASCII。
为了获得正确的计数,您可能需要添加“use bytes;” 将 pragma 添加到脚本顶部,或者将 length 调用包装在一个块中:
来自 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:
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."
您需要的额外字节数是否等于文件中的行数? 我问这个问题是因为也许有可能以某种方式引入了回车符但没有被计算在内。
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.
你如何获得字节数? ..通过查看文件系统上文件的大小?
您可以使用“-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