My question is very simple... How can make an Post HttpRequest with params in angular 4. I could not find an example ... the examples I find are using HttpClient, but it's not what I need
Asked
Active
Viewed 1,996 times
-3
-
2what is wrong with HttpClient? – Pari Baker Oct 15 '18 at 18:55
-
I need a non ajax post request... exactly a full post request – AlejoDev Oct 15 '18 at 18:56
-
@Igor what I need is to redirect the flow of my application to an external service to make the payment of a product .. the external service allows me to make payments for the different means of payment. for that reason I need a non-ajax post – AlejoDev Oct 15 '18 at 19:00
-
Possible duplicate of [How to redirect to an external URL in Angular2?](https://stackoverflow.com/questions/34338440/how-to-redirect-to-an-external-url-in-angular2). (`window.location.href = '...';`) – Igor Oct 15 '18 at 19:01
-
@Igor yes! I want to do a browser redirect – AlejoDev Oct 15 '18 at 19:02
-
I mean if you are doing a browser redirect you can send it as query parameters if it isn't sensitive data. You would still use HttpClient... – Pari Baker Oct 15 '18 at 19:03
-
@Igor I want to do a browser redirect, but with a post request – AlejoDev Oct 15 '18 at 19:05
-
Possible duplicates of: https://stackoverflow.com/q/8389646/1260204, https://stackoverflow.com/q/133925/1260204, https://stackoverflow.com/q/19064352/1260204 – Igor Oct 15 '18 at 19:09
-
so, I would still suggest using a redirect with query params, not entirely sure if its possible to just redirect and post data, assuming this data is not sensitive – Pari Baker Oct 15 '18 at 19:10
-
@PariBaker - `assuming this data is not sensitive` <= I am not sure what that has to do with anything? – Igor Oct 15 '18 at 19:13
-
Seems like a much easier solution for a redirect. He would be able to just pass his query Params through the router as an object. No? – Pari Baker Oct 15 '18 at 19:14
3 Answers
0
Without XHR or fetch (aka AJAX), you're not able to instruct the browser to make an HTTP POST request. By navigating to some URL, the browser makes a HTTP GET request.
If you want to send an HTTP POST request directly from the browser, you'll need to use XHR/fetch.

Raphael Rafatpanah
- 19,082
- 25
- 92
- 158
-
-
-
Does it? I was under the impression that it is a separate thing entirely (though polyfills would certainly use it). Regardless, this is semantics. – jhpratt Oct 15 '18 at 20:04
-
1@jhpratt, Actually I think you're right. They may be two separate things. – Raphael Rafatpanah Oct 15 '18 at 20:07
-
-
@AlejoDev It's already been [shown](https://stackoverflow.com/a/52823170/1911755). Here are the [docs](https://angular.io/guide/http) – Raphael Rafatpanah Oct 15 '18 at 20:20
0
Circling back to this, although I managed to create a test with a get method using the HttpRequest class I was having trouble with using the POST method. Instead I used the HttpModule from @angular/http and the accompanying classes. Then I tried adding an e.preventDefault. Try that and see if it works for you. Additionally you may need to use pure JS methods here.
in AppComonent
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from '@angular/http';
import { Component, OnInit } from "@angular/core";
export class AppComonent implements OnInit {
dataBody =JSON.stringify({
title: "foo",
body: "bar",
userId: 1
});
headers;
requestOptions;
requestMethod;
Response;
constructor(private http: Http) {}
ngOnInit(
) {
}
onPreventDefault(e){
e.preventDefault();
}
onPostData(){
return this.postRequest('https://jsonplaceholder.typicode.com/posts', this.dataBody);
}
postRequest(url,data) {
this.headers = new Headers();
this.headers.append("Content-Type", 'application/json');
this.requestOptions = new RequestOptions({
method: RequestMethod.Post,
url: url,
headers: this.headers,
body: data
});
return this.http.request(new Request(this.requestOptions))
.subscribe((res: Response) => {
if (res) {
console.log(res);
}
});
}
}
in module.ts
import { HttpModule } from '@angular/http';
imports: [HttpModule];
in app-component.html
<form (ngSubmit)="onPostData()" (submit)="onPreventDefault($event)" target="_blank" action="http://localhost:3000/api/users" >
<button type="submit" >Submit</button>
</form>

Pari Baker
- 696
- 4
- 19
-1
in app.module.ts
import { HttpClientModule } '@angular/common/http';
@NgModule ({
imports: [HttpClientModule]
})
in app.component.ts
import { HttpClient } from '@angular/common/http';
export class AppComponent {
constructor(private http: HttpClient) {
this.http.post('http://someurl/endpoint', {postData1: 'avb', postdat2: 'xyx}).subscribe(res => console.log(res), error => console.log(error));
}
}

Vivek Kumar
- 4,822
- 8
- 51
- 85
-
Author asks about providing params in post request, beside that it's in ``HttpClient`` you didn't answer question. – Buczkowski Oct 15 '18 at 19:11