【问题标题】:Slack API Angular Integration 200 error with 'OK response'Slack API Angular Integration 200 错误并带有“OK 响应”
【发布时间】:2021-08-14 08:47:03
【问题描述】:

我正在尝试创建一个与我的angular 应用程序通信的Slack 应用程序,我感觉好像我非常接近让它工作但是我得到了以下错误:

这是我在 angular 应用程序中调用 webhook 的方式:

   this.http.post<any>(webHook, JSON.stringify(message), options).subscribe(res => {
      console.log('res' + res);
      return res;
    }); // error here about json at 0 with ok response and 200 http

我在正确的Slack 频道中看到了通知,但由于我的 Angular 应用程序中有一个错误拦截器,因此错误导致我的 Angular 应用程序出现问题,非常感谢任何帮助。

错误拦截器.ts:

 intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// console.log('request was intercepted by error');

return next.handle(req)
.pipe(
  // Retry on failure
  retry(2),

  // Handle errors
  catchError((error: HttpErrorResponse) => {
    console.error(error);

    let errorMessage = 'An unknown error occurred!';

    if (error.error.message) {
      errorMessage = error.error.message;
    }

    this.dialog.open(ErrorComponent, {data: {message: errorMessage}});
    this.errorService.throwError(errorMessage);

    return throwError(error);
  }));

}

【问题讨论】:

  • 您似乎正在尝试解析已经存在的 JSON 对象?可以加拦截代码吗?
  • @ApoorvaChikara 我已经添加了拦截器,如果我将拦截器全部移除,我仍然会收到错误,所以认为它不是来自这里

标签: angular typescript http slack


【解决方案1】:

解决方案是将请求的resonsetype 更改为'text',因为它期待JSON 响应。

this.http.post(webHook, JSON.stringify(message), {headers, responseType: 'text'}).subscribe(res => {
  console.log('res' + res);
  return res;
});

【讨论】:

    【解决方案2】:

    从服务器返回的答案是正确的,但是返回的时候很可能会返回一个字符串,这会导致它为假。 服务器正确返回信息的示例:

    烘焙:

    return OK('wellcome'); /// return {status:200,ok:false}
    // This is a mistake and you should send the information as follows
    string data='wellcome';
    return Ok(data) // {status:200,ok:true}
    

    【讨论】:

    • 我无法编辑服务器代码,因为它来自 slack api
    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多