cURL脚本创建mongo数据库文档

时间:2018-04-25 09:14:26

标签: bash mongodb curl mlab

我使用Chrome登录我的mLab帐户并创建了一个mongo数据库。然后我打开浏览器devtools并在数据库中创建了一个文档。在devtools的日志中,我看到通过POST" createdocument创建文档?CSRF_TOKEN ...."。当我右键单击此POST时,我可以将命令捕获为cURL cmd或cURL基础。

这是cURL cmd:

curl "https://mlab.com/createdocument?CSRF_TOKEN=SBY9-RTVO-YW1P-LBJ5-EJUY-WYDO-Z7DS-REWE-WOPW-TIQ0-9UW4-LMA4-97IE-6FUS-TDTN-C9N9" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Origin: https://mlab.com" -H "Upgrade-Insecure-Requests: 1" -H "Content-Type: application/x-www-form-urlencoded" -H "User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Mobile Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" -H "Referer: https://mlab.com/databases/test-db/collections/users?_id=_new^&pageSize=10^&pageNum=0^&totalCount=0^&" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: en-US,en;q=0.9" -H "Cookie: __utmc=234932573; __utmz=234932573.1524622777.1.1.utmcsr=(direct)^|utmccn=(direct)^|utmcmd=(none); __utmv=234932573.^|1=account_status=HAS_ACCOUNT=1; __utma=234932573.2132662725.1524622777.1524622777.1524646055.2; __utmt=1; JSESSIONID=1DAFA423980734AB9AB4D6A3A7031E9F.worker2; __utmb=234932573.2.10.1524646055" --data "CSRF_TOKEN=SBY9-RTVO-YW1P-LBJ5-EJUY-WYDO-Z7DS-REWE-WOPW-TIQ0-9UW4-LMA4-97IE-6FUS-TDTN-C9N9^&r=https^%^3A^%^2F^%^2Fmlab.com^%^2Fdatabases^%^2Ftest-db^%^2Fcollections^%^2Fusers^%^3F_id^%^3D_new^%^26pageSize^%^3D10^%^26pageNum^%^3D0^%^26totalCount^%^3D0^%^26^&db=test-db^&collection=users^&id=^&json=^%^7B^%^0D^%^0A++++^%^22fname^%^22^%^3A+^%^22Wile^%^22^%^2C^%^0D^%^0A++++^%^22lname^%^22^%^3A+^%^22Coyote^%^22^%^2C^%^0D^%^0A++++^%^22occupation^%^22^%^3A+^%^22Genius^%^22^%^0D^%^0A^%^7D^&stay=false^&pageSize=10^&pageNum=0^&totalCount=0^&q=^&s=^&f=^&CSRF_TOKEN=SBY9-RTVO-YW1P-LBJ5-EJUY-WYDO-Z7DS-REWE-WOPW-TIQ0-9UW4-LMA4-97IE-6FUS-TDTN-C9N9" --compressed

这是cURL bash:

curl 'https://mlab.com/createdocument?CSRF_TOKEN=SBY9-RTVO-YW1P-LBJ5-EJUY-WYDO-Z7DS-REWE-WOPW-TIQ0-9UW4-LMA4-97IE-6FUS-TDTN-C9N9' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: https://mlab.com' -H 'Upgrade-Insecure-Requests: 1' -H 'Content-Type: application/x-www-form-urlencoded' -H 'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Mobile Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://mlab.com/databases/test-db/collections/users?_id=_new&pageSize=10&pageNum=0&totalCount=0&' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'Cookie: __utmc=234932573; __utmz=234932573.1524622777.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=234932573.|1=account_status=HAS_ACCOUNT=1; __utma=234932573.2132662725.1524622777.1524622777.1524646055.2; __utmt=1; JSESSIONID=1DAFA423980734AB9AB4D6A3A7031E9F.worker2; __utmb=234932573.2.10.1524646055' --data 'CSRF_TOKEN=SBY9-RTVO-YW1P-LBJ5-EJUY-WYDO-Z7DS-REWE-WOPW-TIQ0-9UW4-LMA4-97IE-6FUS-TDTN-C9N9&r=https%3A%2F%2Fmlab.com%2Fdatabases%2Ftest-db%2Fcollections%2Fusers%3F_id%3D_new%26pageSize%3D10%26pageNum%3D0%26totalCount%3D0%26&db=test-db&collection=users&id=&json=%7B%0D%0A++++%22fname%22%3A+%22Wile%22%2C%0D%0A++++%22lname%22%3A+%22Coyote%22%2C%0D%0A++++%22occupation%22%3A+%22Genius%22%0D%0A%7D&stay=false&pageSize=10&pageNum=0&totalCount=0&q=&s=&f=&CSRF_TOKEN=SBY9-RTVO-YW1P-LBJ5-EJUY-WYDO-Z7DS-REWE-WOPW-TIQ0-9UW4-LMA4-97IE-6FUS-TDTN-C9N9' --compressed

我将cURL cmd复制到一个名为create-document.sh的文件中,并在curl之后但在" http .....

之前添加了-i

然后我打开Git Bash(我在Windows 10系统上运行)并执行以下语句:     ./create-document.sh

这是我回复的回复:

$ ./create-document.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   637    0     0  100   637      0   1454 --:--:-- --:--:-- --:--:--  
1454HTTP/1.1 302 Found
Date: Wed, 25 Apr 2018 08:55:54 GMT
Server: Apache
Strict-Transport-Security: max-age=15552000
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Location: https://mlab.com/databases/test-db/collections/users?_id=_new^&pageSize=10^&pageNum=0^&totalCount=0^&
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html;charset=UTF-8

返回的状态是302,我被告知是成功的回复。我期待这个将另一个文档添加到我的数据库中。但是,当我查看数据库时,我只看到原始文档。

然后我删除了create-document.sh的内容并复制到cURL bash命令中。我再次插入-i并保存脚本。我再次执行了./create-document.sh命令,收到状态为302的响应,并检查了mongo数据库。再一次,没有新记录。

有关如何使用cURL命令创建文档的任何建议吗?

谢谢。

0 个答案:

没有答案