Home > Software design >  Why is Angular's PUT sending an empty body?
Why is Angular's PUT sending an empty body?

Time:09-23

I created an object and filled it with some properties using Object.defineProperty then fed that object as an argument for Angular's http.put but for some reason the back end is receiving an empty body.

      let newData = {};

      Object.defineProperty(newData, 'newTitle', {
        value: this.taskForm.value.title,
      });

      Object.defineProperty(newData, 'newDescription', {
        value: this.taskForm.value.description,
      });

      Object.defineProperty(newData, 'newSubtasks', {
        value: this.taskForm.value.subtasks,
      });

      this.http.editTask(this.taskId, newData).subscribe();

editTask implementation

  editTask(taskId: string, data: any) {
    return this.http.put(`http://localhost:3000/edittask/${taskId}`, data);
  }

CodePudding user response:

I'm not prefer to Object.defineProperty but this will solved your problem.

// code

          let newData = {};

          Object.defineProperty(newData, 'newTitle', {
            value: 1,
          });

          Object.defineProperty(newData, 'newDescription', {
            value: 2,
          });

          Object.defineProperty(newData, 'newSubtasks', {
            value: 3,
          });

          const data = {};
          Object.getOwnPropertyNames(newData).forEach(key => data[key] = newData[key]);
          this.http.editTask(this.taskId, data).subscribe();

CodePudding user response:

Try JSON.stringify()

this.http.editTask(this.taskId, JSON.stringify(newData)).subscribe();
  • Related