Home > Software design >  Sending data in json object in angular post request
Sending data in json object in angular post request

Time:12-14

I am working on angular post and try to send a post request with two json object with two diff form bodies in one post request.I have two form but i need to submit the data in one post request.

I and need the post request data in this way.

{
  "detail": {
    "status": "class",
    "service_type": "online",
  },
  "subject": {
    "start_date": "2022-06-28",
    "end_date": "2022-06-29",
    "start_time": "10:00",
    "end_time": "12:30",
  }
}

But After getting the data i am trying to create json object like this. Is this right way to do so?

const body = [{"detail":{"status":"BrakePad","service_type":"online"},"subject":{"start_date": "2022-06-28",} }];

CodePudding user response:

The payload you send and the one you expect to send are not the same : you send an array, but you ask us to send an object.

Your payload should then be :

const body = {
  detail:{
    status: "BrakePad",
    service_type:"online"
  },
  subject: {
    start_date: "2022-06-28"
  } 
};

// Send with 
this.http.post('YOUR_URL', body).subscribe();

CodePudding user response:

You have an extra , in you object.

It should be [{"detail":{"status":"BrakePad","service_type":"online"},"subject":{"start_date": "2022-06-28"} }].

I'm not sure why you are putting the single object into an array. You can post it as {"detail":{"status":"BrakePad","service_type":"online"},"subject":{"start_date": "2022-06-28"} } if the array is not needed.

You will also want to convert your array to a string before posting it using JSON.stringify. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

  • Related