【问题标题】:Post Request return status code 500 Angular request to spring boot backend向 Spring Boot 后端发布请求返回状态代码 500 Angular 请求
【发布时间】:2020-11-25 04:19:33
【问题描述】:

使用以下请求时出现 500 状态码错误

createPost(postPayload: CreatePostPayload): Observable<any> {
    return this.http.post('http://localhost:8080/api/posts/', postPayload);
  }

请求在邮递员上工作正常。

【问题讨论】:

    标签: java angular spring post rest


    【解决方案1】:

    使用 HttpClient 试试。检查你的 post() 正文和标题

    【讨论】:

      【解决方案2】:
       createPost(postPayload: CreatePostPayload): Observable<any> {
              const headers = new HttpHeaders().set('Content-Type', 'application/json');
              return this.http.post('http://localhost:8080/api/posts', postPayload, {    
                  observe: 'response',    
                  headers: headers           
              });
            }
      

      【讨论】:

      • 当我尝试你的代码时,错误从 500 切换到 404
      • 我认为我在后端发现了真正的问题,但我不知道如何解决它,看起来由于某种原因检索当前用户分配帖子时出错至。 java.base/java.lang.String cannot be cast to org.springframework.security.core.userdetails.User
      【解决方案3】:

      所以我终于能够解决这个问题。 事实上,身份验证令牌未包含在发布请求的标头中,这就是它在邮递员上运行良好的原因。感谢 Murugan 和 Ahmed 暗示要检查标题,我设法像这样解决它

        createPost(postPayload: CreatePostPayload): Observable<any> {
      
          const headers = new HttpHeaders().set("Authorization", this.localStorage.retrieve('authenticationToken'));
      
          return this.http.post('http://localhost:8080/api/posts/', postPayload, {    
              observe: 'response',    
              headers: headers           
          });
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-14
        • 1970-01-01
        • 1970-01-01
        • 2019-05-18
        • 2020-04-12
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        相关资源
        最近更新 更多