首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Axios无法附加上一个post请求中的cookie

Axios无法附加上一个post请求中的cookie
EN

Stack Overflow用户
提问于 2021-06-17 16:44:24
回答 3查看 61关注 0票数 1

我正在尝试从axios发送请求,如下所示:

代码语言:javascript
运行
复制
import * as vscode from "vscode";
import * as fs from "fs";
import * as path from "path";
import * as os from 'os';
import axios from 'axios';
import * as FormData from 'form-data';
/**
 * Prefetch QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode
 * Get QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/image
 * Query ScanInfo https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/scan_info
 */
const qrCodeUrl = "https://www.zhihu.com/api/v3/account/api/login/qrcode";
const uDIDUrl = 'https://www.zhihu.com/udid';
export class ZhihuService{
    
    public async login(){
        vscode.window.showInformationMessage("知乎登陆");
        await this.qrcodeLogin();
    }
    protected async qrcodeLogin() {
        var api = axios.create({
            withCredentials: true
        });
        //pre-hand before get qrcode token
        api.post(uDIDUrl, null,{
            withCredentials:true
        }).then(res =>{
            console.log(api);
            console.log(res.data);
        }).catch(err =>{
            console.log(err);
        });
        await api.post(qrCodeUrl, null,{
            withCredentials:true
        }).catch((error: any) =>{
            console.log(error);
        });
    }
}

对于api.post(uDIDUrl,它返回预期结果。

但是对于第二个请求,它总是返回403,如果我在postman中测试这两个reqeust url,它可以检查第二个请求中的错误,我可以看到第一个请求响应中的cookie没有自动附加。

任何建议都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2021-06-29 04:49:02

在我看来,你应该先等第一个电话再打第二个。要么在第一个调用的then子句中执行await,要么执行第二个调用,因为它们相互依赖。

代码语言:javascript
运行
复制
// A Promise will be returned for POST uDIDUrl (never assigned anywhere) and execution will continue
api.post(uDIDUrl, null,{
    withCredentials:true
}).then(res =>{
    console.log(api);
    console.log(res.data);
}).catch(err =>{
    console.log(err);
});

// The execution will block until the returned Promise of the `POST qrCodeUrl` request is fulfilled
await api.post(qrCodeUrl, null,{
    withCredentials:true
}).catch((error: any) =>{
    console.log(error);
});
票数 0
EN

Stack Overflow用户

发布于 2021-06-29 13:00:45

这里的问题是两个API调用是并行运行的。由于API依赖于uDIDUrl's响应,因此您应该先等待第一次qrCodeUrl调用完成。

为此,您有两个选择:

然后在uDIDUrl

  • Use的阻塞等待uDIDUrl请求中进行
  1. 调用。(我更喜欢这个,因为你已经有了异步函数qrcodeLogin)

此外,您不需要再次添加withCredentials,因为您在初始化axios实例时已经编写了一次。

代码语言:javascript
运行
复制
import * as vscode from "vscode";
import * as fs from "fs";
import * as path from "path";
import * as os from 'os';
import axios from 'axios';
import * as FormData from 'form-data';

/**
 * Prefetch QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode
 * Get QRCode https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/image
 * Query ScanInfo https://www.zhihu.com/api/v3/account/api/login/qrcode/${token}/scan_info
 */
const qrCodeUrl = "https://www.zhihu.com/api/v3/account/api/login/qrcode";
const uDIDUrl = 'https://www.zhihu.com/udid';

export class ZhihuService {

  public async login() {
    vscode.window.showInformationMessage("知乎登陆");
    await this.qrcodeLogin();
  }

  protected async qrcodeLogin() {
    var api = axios.create({
      withCredentials: true
    });
    
    //pre-hand before get qrcode token
    await api.post(uDIDUrl).then(res => {      // Added await here & removed extra parameters
      console.log(api);
      console.log(res.data);
    }).catch(err => {
      console.log(err);
    });
    await api.post(qrCodeUrl).catch((error: any) => {
      console.log(error);
    });
  }
}
票数 0
EN

Stack Overflow用户

发布于 2021-06-17 16:48:20

我认为这两个请求是同时发生的,而不是在彼此之后。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68015899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档