Skip to content

Instantly share code, notes, and snippets.

@Hawksbillcat
Last active May 24, 2019 12:36
Show Gist options
  • Save Hawksbillcat/569bde96839787d605f3964bb10f0a16 to your computer and use it in GitHub Desktop.
Save Hawksbillcat/569bde96839787d605f3964bb10f0a16 to your computer and use it in GitHub Desktop.
post in httpclient?
let url = "http://@" + this.sRouterIP + "/config_macfilter_app.php";
let mac:string=("1c:b0:94:ae:0e:1f").toLocaleUpperCase();
let s_body=JSON.stringify({body:"action=app_block&interface=wifi0&mac=" + encodeURIComponent(mac)})
let formdata = new FormData();
formdata.append("body", s_body)
let requestOptions: Object = {
//headers: new HttpHeaders().set('Content-Type','application/x-www-form-urlencoded')//not works
headers: new HttpHeaders().set('Content-Type','application/json'),//not works
responseType: 'text'
}
this.http.post(url,formdata,requestOptions).subscribe((res:any)=>{//put formdata nor s_body object is not working
console.log(res)
})
/////////////////////////WORKS XMLHTTPREQUEST
let sIP = this.sRouterIP;
let url = "http://@" + sIP + "/config_macfilter_app.php";
let sBody = "action=app_block&interface=wifi0&mac=" + encodeURIComponent(mac);
let request = new XMLHttpRequest();
request.open("POST", url,true);
//request.timeout = 30000;
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.onreadystatechange = () => {
if (request.readyState == 4 && request.status == 200) {
if (!this.ValidateResponseText(request.responseText)) {
this.LoadingFailed();
this.CheckConnection();
}
}
}
request.send(sBody);
@amilamen
Copy link

Use this body on your httpClient request : this.http.post(url,body,requestOptions).subscribe((res:any)=>{ console.log(res) })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment