Post request in Angular 6 application2019 Community Moderator ElectionPUT vs. POST in RESTHTTP GET with request bodyHow to process POST data in Node.js?How do I manually fire HTTP POST requests with Firefox or Chrome?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?How to converting Http to HttpClient in Angular 5 (or >4.3)?Angular File Upload to LaravelAngular 5 (Ionic) JSONP requestAngular http requestHttp response is detected but view is not updated using Angular 6.x
My Graph Theory Students
What's the meaning of “spike” in the context of “adrenaline spike”?
If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?
How can you use ICE tables to solve multiple coupled equilibria?
Instead of Universal Basic Income, why not Universal Basic NEEDS?
Opacity of an object in 2.8
In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf
Why Choose Less Effective Armour Types?
Why would a flight no longer considered airworthy be redirected like this?
Why doesn't using two cd commands in bash script execute the second command?
What did Alexander Pope mean by "Expletives their feeble Aid do join"?
How to use deus ex machina safely?
What do Xenomorphs eat in the Alien series?
Why is the President allowed to veto a cancellation of emergency powers?
What are substitutions for coconut in curry?
Interplanetary conflict, some disease destroys the ability to understand or appreciate music
Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?
Who is flying the vertibirds?
Is it possible to upcast ritual spells?
Why do passenger jet manufacturers design their planes with stall prevention systems?
Use of undefined constant bloginfo
Do I need life insurance if I can cover my own funeral costs?
It's a yearly task, alright
Is a party consisting of only a bard, a cleric, and a warlock functional long-term?
Post request in Angular 6 application
2019 Community Moderator ElectionPUT vs. POST in RESTHTTP GET with request bodyHow to process POST data in Node.js?How do I manually fire HTTP POST requests with Firefox or Chrome?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?How to converting Http to HttpClient in Angular 5 (or >4.3)?Angular File Upload to LaravelAngular 5 (Ionic) JSONP requestAngular http requestHttp response is detected but view is not updated using Angular 6.x
In my Angular application I am getting data from an api using a get request in a service I have created, I am trying to create a post request to that api also, my code does not seem to work. my code so far is:
import Injectable from '@angular/core';
import HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse, HttpErrorResponse from '@angular/common/http';
import HttpClientModule from '@angular/common/http';
import Observable, of, throwError from 'rxjs';
import catchError, retry from 'rxjs/operators';
@Injectable(
providedIn: 'root'
)
export class
nowService
serviceApiUrl: string = 'api/incident';
constructor(
private http: HttpClient,
)
getAll(): Observable<any>
return this.http.get<any>(this.serviceApiUrl)
.pipe(
catchError(this.handleError)
);
getIncidents(customerId): Observable<any>
return this.http.get<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
private handleError(error: HttpErrorResponse)
if (error.error instanceof ErrorEvent)
console.log(error.error.message)
else
console.log(error.status)
return throwError(
console.log('Something has happened; Api is not working!'));
;
It takes the customer_id
as a parameter. The error I am getting is:
Expected 2-3 arguments, but got 1.
angular rest http-post
add a comment |
In my Angular application I am getting data from an api using a get request in a service I have created, I am trying to create a post request to that api also, my code does not seem to work. my code so far is:
import Injectable from '@angular/core';
import HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse, HttpErrorResponse from '@angular/common/http';
import HttpClientModule from '@angular/common/http';
import Observable, of, throwError from 'rxjs';
import catchError, retry from 'rxjs/operators';
@Injectable(
providedIn: 'root'
)
export class
nowService
serviceApiUrl: string = 'api/incident';
constructor(
private http: HttpClient,
)
getAll(): Observable<any>
return this.http.get<any>(this.serviceApiUrl)
.pipe(
catchError(this.handleError)
);
getIncidents(customerId): Observable<any>
return this.http.get<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
private handleError(error: HttpErrorResponse)
if (error.error instanceof ErrorEvent)
console.log(error.error.message)
else
console.log(error.status)
return throwError(
console.log('Something has happened; Api is not working!'));
;
It takes the customer_id
as a parameter. The error I am getting is:
Expected 2-3 arguments, but got 1.
angular rest http-post
add a comment |
In my Angular application I am getting data from an api using a get request in a service I have created, I am trying to create a post request to that api also, my code does not seem to work. my code so far is:
import Injectable from '@angular/core';
import HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse, HttpErrorResponse from '@angular/common/http';
import HttpClientModule from '@angular/common/http';
import Observable, of, throwError from 'rxjs';
import catchError, retry from 'rxjs/operators';
@Injectable(
providedIn: 'root'
)
export class
nowService
serviceApiUrl: string = 'api/incident';
constructor(
private http: HttpClient,
)
getAll(): Observable<any>
return this.http.get<any>(this.serviceApiUrl)
.pipe(
catchError(this.handleError)
);
getIncidents(customerId): Observable<any>
return this.http.get<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
private handleError(error: HttpErrorResponse)
if (error.error instanceof ErrorEvent)
console.log(error.error.message)
else
console.log(error.status)
return throwError(
console.log('Something has happened; Api is not working!'));
;
It takes the customer_id
as a parameter. The error I am getting is:
Expected 2-3 arguments, but got 1.
angular rest http-post
In my Angular application I am getting data from an api using a get request in a service I have created, I am trying to create a post request to that api also, my code does not seem to work. my code so far is:
import Injectable from '@angular/core';
import HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse, HttpErrorResponse from '@angular/common/http';
import HttpClientModule from '@angular/common/http';
import Observable, of, throwError from 'rxjs';
import catchError, retry from 'rxjs/operators';
@Injectable(
providedIn: 'root'
)
export class
nowService
serviceApiUrl: string = 'api/incident';
constructor(
private http: HttpClient,
)
getAll(): Observable<any>
return this.http.get<any>(this.serviceApiUrl)
.pipe(
catchError(this.handleError)
);
getIncidents(customerId): Observable<any>
return this.http.get<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId )
.pipe(
catchError(this.handleError)
);
private handleError(error: HttpErrorResponse)
if (error.error instanceof ErrorEvent)
console.log(error.error.message)
else
console.log(error.status)
return throwError(
console.log('Something has happened; Api is not working!'));
;
It takes the customer_id
as a parameter. The error I am getting is:
Expected 2-3 arguments, but got 1.
angular rest http-post
angular rest http-post
edited Mar 6 at 23:34
Trevor
82217
82217
asked Mar 6 at 19:55
SoleSole
4104925
4104925
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
POST request are used when you want to insert something into database. HttpClient post function expects at least 2 arguments. First one is URL and the second one is request body (object you want to insert into DB).
For example if you want to create a user, this is the standard way
this.httpClient.post<any>('/api/user', username: 'test', password: '123');
This will return Observable of the response body (which is probably the same object you passed as second argument with id). If you want the function to return entire response (with status and other useful data), you can pass it third argument (options)
this.httpClient.post<any>('/api/user', ..., observe: 'response');
This will return Observable
which will emit something like this when it completes:
body:
id: 1,
username: 'test',
password: '123'
,
status: 201 // Indicates the creation was successful (sometimes 200),
headers: ... // you can pass them as property of 3rd argument
Since your endpoint doesn't expect the request to have a body, you can do it like this:
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl+"?customer_id="+customerId, null)
.pipe(catchError(this.handleError));
sending data to the server: https://angular.io/guide/http#sending-data-to-the-server
more about http status codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Can you elaborate with code sample?
– Sole
Mar 6 at 19:58
add a comment |
You are missing the body, try adding an empty object
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId, )
.pipe(
catchError(this.handleError)
);
add a comment |
The problem came from the POST method:
return this.http.post<any>(url, body, headers)
However, according to the Angular version that you're working at it may be different.
Angular Docs
add a comment |
By convention, Http POST requests are expected to contain data in the request body. Essentially you're telling the server to create a resource. In order to do that, you put the resource (an incident
in your case) in the request body of the POST request.
To do this in angular, you pass it as the second parameter.
postIncidents(customerId, incident): Observable<any>
const url = this.serviceApiUrl + "?customer_id=" + customerId;
return this.http.post<any>(url, incident)
.pipe(
catchError(this.handleError)
);
And as a general practice, I try to keep the URL as clean as possible with post requests and save the query string parameters for when I'm fetching data. What would be cleanest would be to ensure that customerId
is a property contained in the incident
data. Then it could be cleaned up to just this:
postIncidents(incident): Observable<any>
return this.http.post<any>(this.serviceApiUrl, incident)
.pipe(
catchError(this.handleError)
);
add a comment |
// Example 1:
import HttpClient from '@angular/common/http';
constructor(private http: HttpClient)
serviceurl:any = http://localhost:3000/;
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = `customer_id= $customerId`;
return this.http.post<any>( Url , body, )
.pipe(map(res => res))
.catch(err => err);
Example 2 :
// send data with http headers , content type json
import HttpHeaders from '@angular/common/http';
const httpOptions =
headers: new HttpHeaders(
'Content-Type': 'application/json'
);
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = JSON.stringify(
customer_id: customerId
);
return this.http.post<any>(Url, body, httpOptions)
.pipe(map(res => res))
.catch(err => err);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031219%2fpost-request-in-angular-6-application%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
POST request are used when you want to insert something into database. HttpClient post function expects at least 2 arguments. First one is URL and the second one is request body (object you want to insert into DB).
For example if you want to create a user, this is the standard way
this.httpClient.post<any>('/api/user', username: 'test', password: '123');
This will return Observable of the response body (which is probably the same object you passed as second argument with id). If you want the function to return entire response (with status and other useful data), you can pass it third argument (options)
this.httpClient.post<any>('/api/user', ..., observe: 'response');
This will return Observable
which will emit something like this when it completes:
body:
id: 1,
username: 'test',
password: '123'
,
status: 201 // Indicates the creation was successful (sometimes 200),
headers: ... // you can pass them as property of 3rd argument
Since your endpoint doesn't expect the request to have a body, you can do it like this:
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl+"?customer_id="+customerId, null)
.pipe(catchError(this.handleError));
sending data to the server: https://angular.io/guide/http#sending-data-to-the-server
more about http status codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Can you elaborate with code sample?
– Sole
Mar 6 at 19:58
add a comment |
POST request are used when you want to insert something into database. HttpClient post function expects at least 2 arguments. First one is URL and the second one is request body (object you want to insert into DB).
For example if you want to create a user, this is the standard way
this.httpClient.post<any>('/api/user', username: 'test', password: '123');
This will return Observable of the response body (which is probably the same object you passed as second argument with id). If you want the function to return entire response (with status and other useful data), you can pass it third argument (options)
this.httpClient.post<any>('/api/user', ..., observe: 'response');
This will return Observable
which will emit something like this when it completes:
body:
id: 1,
username: 'test',
password: '123'
,
status: 201 // Indicates the creation was successful (sometimes 200),
headers: ... // you can pass them as property of 3rd argument
Since your endpoint doesn't expect the request to have a body, you can do it like this:
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl+"?customer_id="+customerId, null)
.pipe(catchError(this.handleError));
sending data to the server: https://angular.io/guide/http#sending-data-to-the-server
more about http status codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
Can you elaborate with code sample?
– Sole
Mar 6 at 19:58
add a comment |
POST request are used when you want to insert something into database. HttpClient post function expects at least 2 arguments. First one is URL and the second one is request body (object you want to insert into DB).
For example if you want to create a user, this is the standard way
this.httpClient.post<any>('/api/user', username: 'test', password: '123');
This will return Observable of the response body (which is probably the same object you passed as second argument with id). If you want the function to return entire response (with status and other useful data), you can pass it third argument (options)
this.httpClient.post<any>('/api/user', ..., observe: 'response');
This will return Observable
which will emit something like this when it completes:
body:
id: 1,
username: 'test',
password: '123'
,
status: 201 // Indicates the creation was successful (sometimes 200),
headers: ... // you can pass them as property of 3rd argument
Since your endpoint doesn't expect the request to have a body, you can do it like this:
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl+"?customer_id="+customerId, null)
.pipe(catchError(this.handleError));
sending data to the server: https://angular.io/guide/http#sending-data-to-the-server
more about http status codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
POST request are used when you want to insert something into database. HttpClient post function expects at least 2 arguments. First one is URL and the second one is request body (object you want to insert into DB).
For example if you want to create a user, this is the standard way
this.httpClient.post<any>('/api/user', username: 'test', password: '123');
This will return Observable of the response body (which is probably the same object you passed as second argument with id). If you want the function to return entire response (with status and other useful data), you can pass it third argument (options)
this.httpClient.post<any>('/api/user', ..., observe: 'response');
This will return Observable
which will emit something like this when it completes:
body:
id: 1,
username: 'test',
password: '123'
,
status: 201 // Indicates the creation was successful (sometimes 200),
headers: ... // you can pass them as property of 3rd argument
Since your endpoint doesn't expect the request to have a body, you can do it like this:
postIncidents(customerId): Observable<any>
return this.http.post<any>(this.serviceApiUrl+"?customer_id="+customerId, null)
.pipe(catchError(this.handleError));
sending data to the server: https://angular.io/guide/http#sending-data-to-the-server
more about http status codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
edited Mar 6 at 20:20
answered Mar 6 at 19:57
displayNamedisplayName
426318
426318
Can you elaborate with code sample?
– Sole
Mar 6 at 19:58
add a comment |
Can you elaborate with code sample?
– Sole
Mar 6 at 19:58
Can you elaborate with code sample?
– Sole
Mar 6 at 19:58
Can you elaborate with code sample?
– Sole
Mar 6 at 19:58
add a comment |
You are missing the body, try adding an empty object
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId, )
.pipe(
catchError(this.handleError)
);
add a comment |
You are missing the body, try adding an empty object
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId, )
.pipe(
catchError(this.handleError)
);
add a comment |
You are missing the body, try adding an empty object
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId, )
.pipe(
catchError(this.handleError)
);
You are missing the body, try adding an empty object
return this.http.post<any>(this.serviceApiUrl + "?customer_id=" + customerId, )
.pipe(
catchError(this.handleError)
);
answered Mar 6 at 20:07
uknukn
236111
236111
add a comment |
add a comment |
The problem came from the POST method:
return this.http.post<any>(url, body, headers)
However, according to the Angular version that you're working at it may be different.
Angular Docs
add a comment |
The problem came from the POST method:
return this.http.post<any>(url, body, headers)
However, according to the Angular version that you're working at it may be different.
Angular Docs
add a comment |
The problem came from the POST method:
return this.http.post<any>(url, body, headers)
However, according to the Angular version that you're working at it may be different.
Angular Docs
The problem came from the POST method:
return this.http.post<any>(url, body, headers)
However, according to the Angular version that you're working at it may be different.
Angular Docs
answered Mar 6 at 20:19
IvánIván
738
738
add a comment |
add a comment |
By convention, Http POST requests are expected to contain data in the request body. Essentially you're telling the server to create a resource. In order to do that, you put the resource (an incident
in your case) in the request body of the POST request.
To do this in angular, you pass it as the second parameter.
postIncidents(customerId, incident): Observable<any>
const url = this.serviceApiUrl + "?customer_id=" + customerId;
return this.http.post<any>(url, incident)
.pipe(
catchError(this.handleError)
);
And as a general practice, I try to keep the URL as clean as possible with post requests and save the query string parameters for when I'm fetching data. What would be cleanest would be to ensure that customerId
is a property contained in the incident
data. Then it could be cleaned up to just this:
postIncidents(incident): Observable<any>
return this.http.post<any>(this.serviceApiUrl, incident)
.pipe(
catchError(this.handleError)
);
add a comment |
By convention, Http POST requests are expected to contain data in the request body. Essentially you're telling the server to create a resource. In order to do that, you put the resource (an incident
in your case) in the request body of the POST request.
To do this in angular, you pass it as the second parameter.
postIncidents(customerId, incident): Observable<any>
const url = this.serviceApiUrl + "?customer_id=" + customerId;
return this.http.post<any>(url, incident)
.pipe(
catchError(this.handleError)
);
And as a general practice, I try to keep the URL as clean as possible with post requests and save the query string parameters for when I'm fetching data. What would be cleanest would be to ensure that customerId
is a property contained in the incident
data. Then it could be cleaned up to just this:
postIncidents(incident): Observable<any>
return this.http.post<any>(this.serviceApiUrl, incident)
.pipe(
catchError(this.handleError)
);
add a comment |
By convention, Http POST requests are expected to contain data in the request body. Essentially you're telling the server to create a resource. In order to do that, you put the resource (an incident
in your case) in the request body of the POST request.
To do this in angular, you pass it as the second parameter.
postIncidents(customerId, incident): Observable<any>
const url = this.serviceApiUrl + "?customer_id=" + customerId;
return this.http.post<any>(url, incident)
.pipe(
catchError(this.handleError)
);
And as a general practice, I try to keep the URL as clean as possible with post requests and save the query string parameters for when I'm fetching data. What would be cleanest would be to ensure that customerId
is a property contained in the incident
data. Then it could be cleaned up to just this:
postIncidents(incident): Observable<any>
return this.http.post<any>(this.serviceApiUrl, incident)
.pipe(
catchError(this.handleError)
);
By convention, Http POST requests are expected to contain data in the request body. Essentially you're telling the server to create a resource. In order to do that, you put the resource (an incident
in your case) in the request body of the POST request.
To do this in angular, you pass it as the second parameter.
postIncidents(customerId, incident): Observable<any>
const url = this.serviceApiUrl + "?customer_id=" + customerId;
return this.http.post<any>(url, incident)
.pipe(
catchError(this.handleError)
);
And as a general practice, I try to keep the URL as clean as possible with post requests and save the query string parameters for when I'm fetching data. What would be cleanest would be to ensure that customerId
is a property contained in the incident
data. Then it could be cleaned up to just this:
postIncidents(incident): Observable<any>
return this.http.post<any>(this.serviceApiUrl, incident)
.pipe(
catchError(this.handleError)
);
answered Mar 6 at 21:32
TrevorTrevor
82217
82217
add a comment |
add a comment |
// Example 1:
import HttpClient from '@angular/common/http';
constructor(private http: HttpClient)
serviceurl:any = http://localhost:3000/;
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = `customer_id= $customerId`;
return this.http.post<any>( Url , body, )
.pipe(map(res => res))
.catch(err => err);
Example 2 :
// send data with http headers , content type json
import HttpHeaders from '@angular/common/http';
const httpOptions =
headers: new HttpHeaders(
'Content-Type': 'application/json'
);
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = JSON.stringify(
customer_id: customerId
);
return this.http.post<any>(Url, body, httpOptions)
.pipe(map(res => res))
.catch(err => err);
add a comment |
// Example 1:
import HttpClient from '@angular/common/http';
constructor(private http: HttpClient)
serviceurl:any = http://localhost:3000/;
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = `customer_id= $customerId`;
return this.http.post<any>( Url , body, )
.pipe(map(res => res))
.catch(err => err);
Example 2 :
// send data with http headers , content type json
import HttpHeaders from '@angular/common/http';
const httpOptions =
headers: new HttpHeaders(
'Content-Type': 'application/json'
);
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = JSON.stringify(
customer_id: customerId
);
return this.http.post<any>(Url, body, httpOptions)
.pipe(map(res => res))
.catch(err => err);
add a comment |
// Example 1:
import HttpClient from '@angular/common/http';
constructor(private http: HttpClient)
serviceurl:any = http://localhost:3000/;
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = `customer_id= $customerId`;
return this.http.post<any>( Url , body, )
.pipe(map(res => res))
.catch(err => err);
Example 2 :
// send data with http headers , content type json
import HttpHeaders from '@angular/common/http';
const httpOptions =
headers: new HttpHeaders(
'Content-Type': 'application/json'
);
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = JSON.stringify(
customer_id: customerId
);
return this.http.post<any>(Url, body, httpOptions)
.pipe(map(res => res))
.catch(err => err);
// Example 1:
import HttpClient from '@angular/common/http';
constructor(private http: HttpClient)
serviceurl:any = http://localhost:3000/;
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = `customer_id= $customerId`;
return this.http.post<any>( Url , body, )
.pipe(map(res => res))
.catch(err => err);
Example 2 :
// send data with http headers , content type json
import HttpHeaders from '@angular/common/http';
const httpOptions =
headers: new HttpHeaders(
'Content-Type': 'application/json'
);
postIncidents (customerId): Observable<any>
const Url = `$serviceurl`;
const body = JSON.stringify(
customer_id: customerId
);
return this.http.post<any>(Url, body, httpOptions)
.pipe(map(res => res))
.catch(err => err);
answered Mar 7 at 6:42
arul princearul prince
10116
10116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031219%2fpost-request-in-angular-6-application%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown