0

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Dave
  • 15,639
  • 133
  • 442
  • 830
  • https://angular.io/api/common/http/HttpParams#set - note **new body**, most of the Angular HTTP utility objects are *immutable*. – jonrsharpe May 20 '19 at 21:30
  • Hmm, not sure that will solve the issue, @Dave if it's still a problem open another question. With urlencoded post the params should be in the body. – peekay May 20 '19 at 21:56

0 Answers0