如Chrome Inspect所述,如何复制发布请求?

时间:2019-04-01 17:25:12

标签: javascript api cookies axios postman

我正在尝试通过编程方式对英超联赛幻想游戏(https://fantasy.premierleague.com/)进行身份验证。登录时在Chrome Inspect中检查“网络”标签时,看到以下发布请求和响应。

常规:
要求网址:https://users.premierleague.com/accounts/login/
请求方法:POST
状态码:302
远程地址:151.101.86.217:443
推荐人政策:no-referrer-when-downgrade

响应标题:
接受范围:字节
缓存控制:私有,无缓存,无存储,必须重新验证,最大年龄= 0
内容长度:0
内容类型:text / html; charset = utf-8
日期:2019年3月31日,星期日,格林尼治标准时间17:22:05
位置:https://fantasy.premierleague.com/a/login?state=success
服务器:nginx / 1.13.5
set-cookie:csrftoken = cjda9XUQ26ZiXWl0KUbk5GVNNVUSUQZCy70OMZn4TcjapME8pijv7cNI413bPHs8; expires = Sun,2020年3月29日17:22:05 GMT;最大年龄= 31449600;路径= /
set-cookie:messages =“ fa4474ff71c7bd5422bf64125057fbc6ed68c6b1 $ [[\” __ json_message \“ \ 0540 \ 05425 \ 054 \”已成功登录为。\“] \ 054 [\” ____ json_message \“ \ 0540 \ 05425 \ 054 \” \“] \ 054 [\” __ json_message \“ \ 0540 \ 05425 \ 054 \”已成功登录为。\“]]”; httponly;路径= /
set-cookie:sessionid = mruasqjsv9ru8r7qlqk5vauixkt3uq88; expires =星期日,2019年4月14日17:22:05 GMT; httponly;年龄上限= 1209600;路径= / set-cookie:sudo =“ V1mmxfTng2qS:1hAe9l:Kn0n8h3dSwGH-Qv2RbGNn5MrwvE”; expires =星期日,2019年3月31日20:22:05 GMT; httponly;最大年龄= 10800;路径= /;安全
设置cookie: Domain = premierleague.com; expires =星期日,2019年4月14日17:22:05 GMT;年龄上限= 1209600;路径= /;安全
状态:302
不同:Cookie
通过:1.1 google
通过:1.1清漆
x缓存:MISS
x-cache-hits:0
x-frame-options:SAMEORIGIN
x-served by:cache-bma1634-BMA

请求标头
:authority:users.premierleague.com
:方法:POST
:path:/ accounts / login /
:方案:https
接受:text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng, / ; q = 0.8
accept-encoding:gzip,deflate,br
接受语言:sv-SE,sv; q = 0.9,en-US; q = 0.8,en; q = 0.7
快取控制:max-age = 0
内容长度:186
内容类型:application / x-www-form-urlencoded
Cookie:_ga = GA1.2.963283199.1544268407; _gid = GA1.2.530727258.1554052864; csrftoken = xRErJTDda0drIGPICOjvXMHhkjjRRmsO74wbhoZBmKbz5zpHATuVJQ3sTWIIvaXf; messages =“ 956228cd3e90b2b498670e8d2101699bcec07800 $ [[\” __ json_message \“ \ 0540 \ 05425 \ 054 \”已成功登录为。\“] \ 054 [\” __ json_message \“ \ 0540 \ 05425 \ 054 \”您已退出。\ “]]”; _gat = 1; _gat_UA-33785302-1 = 1
来源:http://evil.com/
推荐人:https://fantasy.premierleague.com/?fail
升级不安全请求:1
用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_11_6)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 72.0.3626.121 Safari / 537.36

表格数据:
csrfmiddleware令牌:OFwVFKPSkKc0AG1hxHekpCSWb1l3Wl35
登录名:
密码:
应用:plfpl-web
redirect_uri:https://fantasy.premierleague.com/a/login


我正在尝试使用Javascript和Axios复制此发布请求。我有以下代码:

authPLfantasy() {
        let formData = new FormData();
        formData.append('password', '<password>');
        formData.append('login', '<username>');
        formData.append('redirect_uri', 'https://fantasy.premierleague.com/a/login');
        formData.append('app', 'plfpl-web');

        let config = {
            url: 'https://users.premierleague.com/accounts/login',
            method: 'post',
            withCredentials: true,
            data: formData
        };

        return axios.request(config);
    }

记录该函数的结果时得到的结果是:“从原点'https://users.premierleague.com/accounts/login'对'http://localhost:3000'处XMLHttpRequest的访问已被CORS策略阻止:'Access的值当请求的凭据模式为“ include”时,响应中的“ -Control-Allow-Origin”标头不得为通配符“ *”。XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制。”

我对CORS有点陌生,所以我想如果我将头撞到墙上足够长的时间并用力敲击就可以解决,但是我听说通过Postman发送发帖请求不会受到CORS的影响,所以我尝试使用它相反,只是看我是否可以使它工作。当我使用Postman收到200 OK响应时,我没有得到正确的响应,严重的是我缺少cookie pl_profile和sessionid。在Postman中更改了表单数据,以使密码或用户名无效仍然会显示200 OK响应。

最终目标是使用Javascript在用户单击我的React应用程序中的按钮后提交必要的凭据后发送发布请求。应该生成必要的cookie,以便应用程序随后可以将get请求发送到另一个受auth保护的端点,并从那里检索信息。我觉得这不应该那么难,但是我似乎无法使其工作。

1 个答案:

答案 0 :(得分:0)

如果您使用的是Chrome,也许最简单的方法是

  1. 打开Chrome开发工具
  2. 转到“网络”标签
  3. 选择您要复制的请求
  4. 右键单击并选择“复制>复制为cURL”
  5. 打开终端,粘贴命令并执行

enter image description here