I'm trying to POST form data with Angular 7. The receiving service expects a parameter (named todostr
) with a string value. Below I tried
export class ComponentService {
private apiUrl = 'http://localhost:8080/todo/';
constructor(private http: HttpClient) {
}
...
createTodo(todo: string): Promise<Array<AppComponent>> {
let todoHeaders = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
let params = new HttpParams();
params.set('todostr', todo);
return this.http.post(this.apiUrl + "add", null, { headers: todoHeaders, params: params })
.toPromise()
.then(response => response as AppComponent[])
.catch(this.handleError);
But I get a 400 from my microservice because the parameter todostr
is not being sent. I left null
in the second param for the this.http.post
method because I don't have a JSON body (the microservice won't accept JSON and I can't change the microservice).
What's the right way to POST data using Angular?