Whenever I attempt to send a post request to Django server, I encounter a 500 (Internal Server Error) response. Interestingly, the get and put requests work flawlessly on the same server where Django is connected to PostgreSQL database.
Here is a snippet of the component.ts file:
export class UsersComponent implements OnInit{
users!: any[]
user:any = {
fullname:'',
email:'',
car:''
};
constructor(public userService: UserService, private route: ActivatedRoute){}
ngOnInit(): void {
this.getUserData()
}
onCreate(){
this.userService.createUser(this.user).subscribe(
(response:any) =>{
console.log('user created succesfully');
this.getUserData();
},
(error:any)=>{
console.error('errror',error);
}
);
}
getUserData(){
this.userService.getUsers().subscribe((data:any) => {
this.users = data
})
}
}
And here is a glance at the service.ts file:
export class UserService {
private apiUrl = 'http://127.0.0.1:8000/api/user'
constructor(public http: HttpClient) { }
createUser(user:any){
return this.http.post(this.apiUrl, user)
}
}
In my Django models.py file, you can find the following model definition:
class User(models.Model):
fullname = models.CharField(max_length=255)
email = models.EmailField(max_length=55)
car = models.CharField(max_length=255)