在有角度的帖子请求中收到400个错误的请求,我该如何解决

时间:2020-05-16 14:47:18

标签: django angular django-rest-framework

请我在我的angular和django项目上需要帮助,我发出发布请求时遇到400错误的响应错误,项目工作流程如下,用户注册(成功)然后重定向到一份个人资料表格,当我在填写个人资料后尝试发出发布请求时,我得到了400 err,下面是我的代码和d err响应

角度服务.ts

    addProfile(profile: Iprofile): Observable<Iprofile>{
        const url = 'http://127.0.0.1:8000/user/profiles';
        return this.httpClient.post<Iprofile>(url, profile)
            .pipe(
                 map(response => response),
                 catchError(this.handleError)
            );
    }

创建配置文件component.ts

    addUpdateProfile(){
    if (this.profile) {
        this.dataService.updateProfile(this.profileForm.value)
        .subscribe(item => this.profiles.push(this.profileForm.value));
    }
    else {
        this.dataService.addProfile(this.profileForm.value)
        .subscribe(item => this.profiles.push(this.profileForm.value));
    }
  }

django网址

path('profiles', CreateProfileView.as_view(), name='user-profile'),

django视图

class CreateProfileView(generics.ListCreateAPIView):
queryset = Profile.objects.all()
serializer_class = UserDetailSerializer

错误响应

错误请求:/ user / profiles [14 / May / 2020 17:07:04]“ POST / user / profiles HTTP / 1.1” 400 46

如果有人能帮助我,b会很高兴

1 个答案:

答案 0 :(得分:1)

documentation中,它表示“如果提供的用于创建对象的请求数据无效,则将返回400 Bad Request响应,并将错误详细信息作为响应的主体。”因此,您发送的个人资料无效。要查看哪个部分不对,请登录正文,或者您可以覆盖并调试CreateApiView

的post方法