When sending the body of an http post request in Angular, I typically use the following approach:
let requestBody: String = "";
//dataObject is the object containing form values to send
for (let key in dataObject) {
if (dataObject[key]) {
requestBody += (body.length ? '&' : '') + key + "=" + dataObject[key];
}
}
After assembling the requestBody
, I include it in my http post request like this:
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let requestOptions = new RequestOptions({ headers: headers });
//http represents an Http instance
http.post(URI, requestBody, requestOptions)
I'm curious if there is a more efficient way or a built-in method that allows for the direct usage of the dataObject
within the post method rather than the aforementioned implementation.