I\'m trying to do an api call from my angular app. What I want to do is send a post request to the api with a command param. I have done a lot of server side testing as well as
The 2nd parameter of http.post is the body of the message, ie the payload and not the url search parameters. Pass data
in that parameter.
From the documentation
post(url: string, body: any, options?: RequestOptionsArgs) : Observable
public post(cmd: string, data: object): Observable {
const params = new URLSearchParams();
params.set('cmd', cmd);
const options = new RequestOptions({
headers: this.getAuthorizedHeaders(),
responseType: ResponseContentType.Json,
params: params,
withCredentials: false
});
console.log('Options: ' + JSON.stringify(options));
return this.http.post(this.BASE_URL, data, options)
.map(this.handleData)
.catch(this.handleError);
}
You should also check out the 1st parameter (BASE_URL
). It must contain the complete url (minus query string) that you want to reach. I mention in due to the name you gave it and I can only guess what the value currently is (maybe just the domain?).
Also there is no need to call JSON.stringify
on the data/payload that is sent in the http body.
If you still can't reach your end point look in the browser's network activity in the development console to see what is being sent. You can then further determine if the correct end point is being called wit the correct header and body. If it appears that is correct then use POSTMAN or Fiddler or something similar to see if you can hit your endpoint that way (outside of Angular).