I have the following problem. I have a server on which the API is, I send the request to the registration endpoint, but in response I get a 400 Bad Request error, stating that the name, surname, email etc must be filled out. The problem is that they are filled. I miss ideas anymore. My code:
import {Component} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Brak tytułu';
constructor(private http: HttpClient) {
}
registerUser() {
const config = {headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')};
const user: User = {
name: 'Pawel',
surname: 'Tester',
email: 'tester@wp.pl',
password: 'loremipsum12',
password_confirm: 'loremipsum12',
event_id: 1,
};
this.http.post('https://myapiserver', user, config).subscribe((res) => {
console.log(res);
console.log(user);
},
error => {
console.log(error);
console.log(user);
});
}
}
interface User {
name: string;
surname: string;
email: string;
password: string;
password_confirm: string;
event_id: number;
}
This code works fine, but he is angularJS
// User create
$scope.createUser = function()
{
var create_req = $rootScope.reqDefaults;
create_req.method = 'POST';
create_req.url = $rootScope.api+'/admin/user';
create_req.data = $httpParamSerializerJQLike($scope.current);
create_req.transformRequest = angular.identity;
// Users list
$http(create_req)
.then(function(response){
$location.path('/users');
$.notify({
title: '<strong>Użytkownik utworzony.</strong>',
message: $scope.current.name+' '+$scope.current.surname+' (ID: '+response.data.id+')'
},{
type: 'success'
});
return true;
}, function(response){
$rootScope.rspError(response.data);
return false;
});
};
If i send request to the register endpoint from Postman, all works fine, my user is register correctly :( I set content type to application/x-www-form-urlencoded.