如何从 Angular 中的 404 Http 错误中获取 Set-Cookies?

发布于 2025-01-15 01:23:48 字数 1769 浏览 4 评论 0原文

大家好,我想登录网站。

我在此网址上发布带有有效电子邮件和密码的 formData https://www.imo.net/ members/imo_registration/login/ 和响应返回 404 错误,这一切“正确”(我不创建此服务器,但如果我在此服务器上有效登录。服务器返回 404 页面。更多图片)。

正如你所看到的,我有效地登录了这个页面,但我想从这个响应中获取 Set-Cookie。我该怎么做?

单击按钮上的登录事件:

async onLogin(id: number) {
    await this.componentsUtils.loadingShow();

    this.imoService.login().pipe(
      tap((data: any) => {
        // not print
        console.log("data", data);
      }),
      finalize(() => this.componentsUtils.loadingHide()),
    ).subscribe(
      res => {
        // not print
        console.log('HTTP res', res);
      },
      err => {
        // here i catch error
        console.log('HTTP Error', err)
      },
    );
 }

ImoService:

export class ImoService {

  private resourceUrl: string = "https://www.imo.net";

  constructor(
    private http: HttpClient,
  ) { }

  login(email: string = "[email protected]", password: string = "somePassword"): Observable<any> {
    const formData: any = new FormData();
    formData.append('email', email);
    formData.append('password', password);

    return this.http.post<any>(this.resourceUrl + "/members/imo_registration/login/", formData);
  }
}

控制台日志:在此处输入图像描述

网络:在此处输入图片描述

有效登录后的页面:在此处输入图片描述

Hello guys I want login to website.

I post formData with valid email and password on this Url https://www.imo.net/members/imo_registration/login/ and response return 404 error and this all "right" (I dont make this server but if i valid login on this server. Server return 404 with page. More in images).

As you can see i valid login this page but i want from this response get Set-Cookie. How can i do it?

Login event onClick button:

async onLogin(id: number) {
    await this.componentsUtils.loadingShow();

    this.imoService.login().pipe(
      tap((data: any) => {
        // not print
        console.log("data", data);
      }),
      finalize(() => this.componentsUtils.loadingHide()),
    ).subscribe(
      res => {
        // not print
        console.log('HTTP res', res);
      },
      err => {
        // here i catch error
        console.log('HTTP Error', err)
      },
    );
 }

ImoService:

export class ImoService {

  private resourceUrl: string = "https://www.imo.net";

  constructor(
    private http: HttpClient,
  ) { }

  login(email: string = "[email protected]", password: string = "somePassword"): Observable<any> {
    const formData: any = new FormData();
    formData.append('email', email);
    formData.append('password', password);

    return this.http.post<any>(this.resourceUrl + "/members/imo_registration/login/", formData);
  }
}

Console log: enter image description here

Network: enter image description here

Page after valid login: enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2025-01-22 01:23:48

当您发出请求时,您必须向 httpClient 传递一个附加参数。

return this.http.post<any>(this.resourceUrl + "/members/imo_registration/login/", formData, {observe: 'response'});

请注意,该方法现在返回一个 HttpResponse 对象,其中包含来自服务器的所有响应,而不仅仅是正文。

您可以像这样访问“Set-Cookie”标头

resp.headers.get('Set-Cookie')

,并且可以使用以下命令访问响应的正文

resp.body

You have to pass an additional argument to the httpClient when you are making the request.

return this.http.post<any>(this.resourceUrl + "/members/imo_registration/login/", formData, {observe: 'response'});

Be careful that the method now returns an HttpResponse object containing all the response from the server and not just the body.

You can access the 'Set-Cookie' header like this

resp.headers.get('Set-Cookie')

and you can access the response's body with

resp.body
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文