我试图在Crystal中构建CLI工具以send-anywhere.com
从命令行使用。
发送分段不是Crystal内置的,但是在编写自己的文件之前,我想我会尝试使用cURL来查看我应该如何做,但是我什至无法使它与cURL一起使用!
问题是,当使用cURL发送多个文件时,他们的服务器仅看到1个文件传入,他们确实看到了总长度,但由于只希望一个文件,因此失败了50%。
使我丧命的是它可以在浏览器中运行,我打开了网络检查器,但看不到cURL请求的区别。我尝试将Expect标头设置为100-continue,对它们进行了比较,但是我看不到有什么可以使它与浏览器一起工作而不卷曲。
这是我使用cURL尝试的所有命令,结果相同,服务器最终只看到1个文件传入,而不是2个。
为了进行测试,我使用了一些通用许可文件的副本。
curl -F file1=@LICENSE -F file2=@LICENSE1 https://...their.weblink...
我在检查器中看到Chrome在Content-Disposition中命名为文件名=“ file []”,所以我自己尝试了一下(结果相同):
curl -F file[]=@LICENSE -F file[]=@LICENSE1 https://...their.weblink...
我也尝试了使用-H "Expect: 100-continue"
相同结果的这两个命令。
在这一点上,我发了疯,以为自己会自己尝试,也许cURL不能正确执行(非常不可能的imo,我做错事的可能性更大)。
因此,在从头开始编写之前,我尝试了Telegram机器人使用的实现,请参见此处:https : //github.com/hangyas/TelegramBot/blob/b3fcbbb621bd669bbafe9f3e91364702d06d1e10/src/TelegramBot/http_client_multipart.cr
这很简单,但是我仍然遇到同样的问题。仅第一个文件被识别。
注意:当仅发送一个文件时,一切都可以与cURL和Crystal实现一起使用。
我快疯了,可以正常使用的浏览器与其他两个浏览器有什么区别?我没看到什么?
我不是在寻找实现,而是只是为了指出某人我错过的东西,这将使多个文件被正确识别?
这仅出于教育目的,实际上这样做违反了服务条款。给定一个API密钥后,它具有文档化的API,您应该改用它。
key
正确向端点声明文件数量很重要。因此,整个流程如下所示:
#!/bin/bash
# First we need to get a device key by registering ourselves as a
# device. For that we need a profile name. We need to store the
# received cookie and send it with the subsequent request.
profilename="$(openssl rand -hex 6)"
curl -c .session -vL https://send-anywhere.com/web/device \
-d "os_type=web" \
-d "profile_name=$profilename" \
-d "manufacturer=Linux" \
-d "model_number=Firefox" \
-d "app_version=48" \
-d "device_language=en-US"
# We need to know the individual filesizes in bytes as well as
# the total size we're going to upload
file0name="foo.txt"
file0size="$(wc -c "$file0name" | cut -d ' ' -f 1)"
file1name="bar.txt"
file1size="$(wc -c "$file1name" | cut -d ' ' -f 1)"
filesize="$(echo "$file0size + $file1size" | bc)"
# Using that info and the cookie we got from the device key
# we can correctly announce our upload
key="$(curl -b .session -vL https://send-anywhere.com/web/key \
-d "file[0][name]=$file0name" -d "file[0][size]=$file0size" \
-d "file[1][name]=$file1name" -d "file[1][size]=$file1size" \
-d "file_number=2" -d "file_size=$filesize")"
# We get some JSON back with the URL to send to the receiver
# and the URL to upload back
url="$(echo "$key" | ruby -rjson -e 'print JSON.parse($stdin.read)["link"]')"
upload_url="$(echo "$key" | ruby -rjson -e 'print JSON.parse($stdin.read)["weblink"]')"
echo
echo "------------------------------------------------------------"
echo "Receive 2 files of $filesize bytes at $url"
echo "------------------------------------------------------------"
echo
# And finally do the upload
curl -vL "$upload_url" \
-F "file[]=@$file0name" \
-F "file[]=@$file1name"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句