以下是REST API Laravel服务器的completely reproducible个docker镜像:
FROM php
RUN apt-get update
RUN apt-get install zip -y
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN composer create-project laravel/laravel frontend
WORKDIR frontend
COPY web.php web.php
RUN mv web.php routes
EXPOSE 8000
CMD [ "php", "artisan", "serve", "--host", "0.0.0.0" ]
我唯一编辑的是一个端点(web.php
):
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return csrf_token();
});
Route::post('/', function (Request $request) {
if ($request->hasFile('source' )) { return ">> GOOD ! <<"; }
if ($request->hasFile('input.json')) { return ">> GOOD ! <<"; }
$x = count($_FILES);
return "DD >> $x << DD";
});
我构建并运行docker映像,如下所示:
$ docker build --tag host.translator.php --file Dockerfile .
$ docker run -p 8008:8000 -d -t --name translator.php host.translator.php
然后就像相关文章说的(here,and here,and also here):
$ Set-Variable -Name X -Value (curl.exe -X GET -F "source=@input.json" http://127.0.0.1:8008/)
# X holds the token:
# echo $X
# U263lUv3dz9PH7ySm3GmgUUjHjjWm78cqofODUmh
但当我try 使用该令牌时,它不起作用:
$ curl.exe --header "X-CSRFToken:$X" -X POST -F "source=@input.json" http://127.0.0.1:8008/ | Select-String "<title>Page Expired"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6851 0 6609 100 242 80486 2947 --:--:-- --:--:-- --:--:-- 83548
<title>Page Expired</title>
如有任何帮助,我们将不胜感激,谢谢!
EDIT:
#
# used a new variable Z - just to be sure ...
#
$ Set-Variable -Name Z -Value (curl.exe -b cookiejar -X GET -F "source=@input.json" http://127.0.0.1:8008/)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 282 0 40 100 242 527 3194 --:--:-- --:--:-- --:--:-- 3760
$ echo $Z # this looks good, right ?
aHvU5WOaGs9qxOIzgdYb90TJUKPDgDaNhYjQNALW
#
$ type .\cookiejar # this looks ... hmmm ... good (I think ?)
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_127.0.0.1 FALSE / FALSE 1711103819 laravel_session eyJpdiI6IllsOW9KdlVzT1ZPcUJDOUdQRnUyZHc9PSIsInZhbHVlIjoiekpING13WFRaaDRLUHBUcVpNM0VjblBwVjlXM1VrSU1uYUF3dk43d0J6c0RvSDJ5UWZOU3lla1lKVjBNRnNVTXJOaTNMdmt3dlIvdW5HZDk1ODZUb0gxVWMySHh5cXBWeG5XbjVMQlpMQ2xoMzM0ODROQ1FGQ0NMQzFNaHNQMXMiLCJtYWMiOiI0MTg3ZTM0NTlmNWMzZGUyZDY3YzI2YWViYzc2MDZhMjAyMjA2NzE5YmRmYTRmYWRmYzQxODIzYmVlOThlZTkxIiwidGFnIjoiIn0%3D
#
# Now let's cross our fingers extra strong !
#
$ curl.exe -c cookiejar --header "X-CSRFToken:$Z" -X POST -F "source=@input.json" http://127.0.0.1:8008/ | Select-String "<title>Pa
ge Expired"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6851 0 6609 100 242 85705 3138 --:--:-- --:--:-- --:--:-- 87833
242 83137 3044 --:--:- <title>Page Expired</title>- --
:--:-- --:--:-- 85637