Axios 不发起 POST 请求

时间:2021-05-22 23:11:53

标签: axios webdriver-io

我正在使用 webdriverIO 版本 7 和 axios,以便尝试通过 API 进行登录,而不是使用 UI 进行登录。
这是我的代码:

getAuthToken({ email, password }) {

    // axios
    //   .post('https://my-app.com/login', {
    //     j_username: email,
    //     j_password: password,
    //     CSRFToken: 'some-token',
    //   })
    //   .then((response) => {
    //     console.log('XXX');
    //     console.log(response);
    //   });

    const data = {
      j_username: email,
      j_password: password,
      CSRFToken: 'some-token',
    };
    axios({
      method: 'POST',
      headers: { 'content-type': 'application/x-www-form-urlencoded' },
      data: qs.stringify(data),
      url: 'https://my-app.com/login',
    }).then((response) => {
      console.log('XXX');
      console.log(response);
    });
  }  

我正在尝试以上述两种方式进行操作,但我没有在控制台中打印任何响应。
我尝试通过 Postman 执行请求,并且工作正常。

此外,我正在通过 Fiddler Everywhere 应用程序监控网站上的流量,当执行此方法时,Fiddler 中不会显示任何内容。
另一方面,当我通过 Postman 执行此操作时,Fiddler 会发现它。

这是原始邮递员请求数据:

POST https://my-app.com/j_spring_security_check HTTP/1.1
User-Agent: PostmanRuntime/7.28.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 39311680-b11c-4a65-8ff7-2f03b97bf5eb
Host: my-app.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------619522728182415185770824
Cookie: anonymous-consents=%5B%5D; cookie-notification=NOT_ACCEPTED
Content-Length: 436

----------------------------619522728182415185770824
Content-Disposition: form-data; name="j_username"

email@test.com
----------------------------619522728182415185770824
Content-Disposition: form-data; name="j_password"

123456
----------------------------619522728182415185770824
Content-Disposition: form-data; name="CSRFToken"

some-token
----------------------------619522728182415185770824--

这是我通过 Chrome 执行时的原始请求

POST https://my-app.com/j_spring_security_check HTTP/1.1
Host: my-app.com
Connection: keep-alive
Content-Length: 90
Cache-Control: max-age=0
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
sec-ch-ua-mobile: ?0
Upgrade-Insecure-Requests: 1
Origin: https://my-app.com
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: https://my-app.com/login
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: JSESSIONID=some_id; dtCookie=some_data; anonymous-consents=%5B%5D; cookie-notification=NOT_ACCEPTED

j_username=ecx%40test.com&j_password=123456&CSRFToken=some_token

我做错了什么?为什么它在通过 axios 执行时从不记录响应?
谢谢!

0 个答案:

没有答案
相关问题