Reputation: 46479
I am using angular2 http.post
in the following service:
import {Injectable, Inject} from 'angular2/core'
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from 'angular2/http';
import 'rxjs/add/operator/map';
//Globals
const URL : string = 'http://myurl.com/listings';
@Injectable()
export class ListingsService {
//Constructor
constructor(public http: Http) {}
postListing(data) {
this.http.post(URL, data).map(res => res.json())
}
}
The issue is that I am only allowed to post data as a string inside http.post
, where as I need to pass in an Object.
Upvotes: 2
Views: 1392
Reputation: 6325
Probably you need to set Content-Type in your header. You can set default header as follows.
class MyOptions extends RequestOptions {
constructor() {
super({
headers: new Headers({
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json'
})
});
}
}
bootstrap(AppComponent, [
...,
provide(RequestOptions, {useClass: MyOptions})
]);
Upvotes: 1
Reputation: 59
as binariedMe mentioned, JSON.stringify will do what you want. If you want to send binary data maybe you want to look into BSON: https://github.com/mongodb/js-bson
Upvotes: 0
Reputation: 4329
One way of doing this is using JSON.stringify for making a string of object and then JSON.parse on the server side.
Upvotes: 5