Is it possible to send only the PHPSESSID when I request my api? I ask this because in the request I make (attached image) I send the cookie auth and I do not need to send it.
Request code
this.token = this.cookies.getCookie("auth");
let headers = new Headers({'Content-Type': 'text/html', 'Authorization': 'Bearer ' + this.token});
let options = new RequestOptions({ headers: headers, body: '', withCredentials: true});
return this.http.get(this.urlbase + "pesquisa/"+pesquisa+"/"+pag , options)
.map((response: Response) => response.json());
Related
i try to send post request in flutter app like
static Future<List<dynamic>?> postData(data) async {
var body = json.encode(data);
Map<String, String> headers = {"Content-Type": "application/json"};
var url = Uri.http(Config.api, Config.endPoint);
var response = await client.post(url, headers: headers, body: body);
if (response.statusCode == 201) {
var data = jsonDecode(response.body);
return data;
}
return null;
}
this is data was sent
Map data = {
'database': 'school_control_ykt',
'table': 'tablets_helper',
'place': place,
'reason': reason,
'teacher': teacher,
'name': name,
'id_group': id_group
};
postData(data);
but in server side (php) $_POST are empty
i wanna know why $_POST are empty when a send the request in flutter app but in Postman request is send successfully and $_POST have data
enter image description here
Use MultipartRequest as API expect multipart/form-data. Something like this:
var request = MultipartRequest('POST', uri)
..fields = data
var response = await request.send();
I already developed ionic 4 in my Mac. When trying to improve an app in ionic 3, my http.post (angular) calls in php is received as GET ($ _SERVER ['REQUEST_METHOD']). I discovered that my parameters are not being recognized in php.
* I read the other posts and none solved my problem
Thank you!
import { Http , Headers, RequestOptions} from '#angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
let parametros = JSON.stringify({
idtApostador: localStorage.getItem("idtApostador"),
});
let headers = new Headers(
{
'Content-Type' : 'application/json'
});
let options = new RequestOptions({ headers: headers });
return new Promise(resolve => {
this.http.post(url, parametros, options)
.timeout(15000)
.subscribe(data => {
})
})
I ask, I answer. :)
https instead of http.
Use
import { HttpClient } from '#angular/common/http';
Instead of HTTP
I am trying to upload image using below function everything working fine only is i want to send image in post and when i am trying get image getting nothing
This is for call API
Future getUploadImg(access_token,File _image) async {
print("Image: $_image");
String apiUrl = '$_apiUrl/user/upload-profile-image';
final length = await _image.length();
final request = new http.MultipartRequest('POST', Uri.parse(apiUrl));
request.headers['Accesstoken'] = "Bearer $access_token";
request.files.add(new http.MultipartFile('imagefile',_image.openRead(), length));
http.Response response = await http.Response.fromStream(await request.send());
print("Result: ${response.body}");
return json.decode(response.body);
}
My file that is passing to server is:
File: '/storage/emulated/0/Android/data/com.dotsquares.ecomhybrid/files/Pictures/c5df03f7-097d-47ca-a3c5-f896b2a38c086982492957343724084.jpg'
I got the result finally we need to pass string for image sharing my working code if anyone need for help:
Future getUploadImg(access_token,File _image) async {
print("Image: $_image");
var result;
var stream = new http.ByteStream(DelegatingStream.typed(_image.openRead()));
var length = await _image.length();
var uri = Uri.parse('$_apiUrl/user/upload-profile-image');
var request = new http.MultipartRequest("POST", uri);
request.headers['Accesstoken'] = "Bearer $access_token";
var multipartFile = new http.MultipartFile('imagefile', stream, length, filename: basename(_image.path));
request.files.add(multipartFile);
var response = await request.send();
print(" ===================response code ${response.statusCode}");
await response.stream.transform(utf8.decoder).listen((value) {
print(" =====================response value $value");
result = value;
});
return json.decode(result);
}
To avoid the following problems:
write failed
connection closed before ...
Flutter Doctor Error - SocketException: Write failed (OS Error: Broken pipe, errno = 32)
you need to add the correct parameters in the headers.
In my case, these problems occur with uploading images and sending base64 encoded requests. I solved it by adding the following 'connection' header: 'keep-alive':
final response = await this.httpClient.put(
url,
encoding: Utf8Codec(),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Accept': "*/*",
'connection': 'keep-alive',
'Accept-Encoding' : 'gzip, deflate, br',
},
body: body,
);
I have imported following module
import 'rxjs/add/operator/toPromise';
import {Http,Headers,RequestOptions} from '#angular/http';
and i have set in headers are
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
and make call like this
this.apiUrl ="some api url here";
return this.http.get(this.apiUrl+'user-details.php?e='+email+'&p='+password,{headers: headers})
.map(res => res.json())
.toPromise()
.catch(this.handleError);
it's working good on browser and returns result
But when i run on device it shows error as "response with status: 0 for url: null".
please guide me to fix this issue
$_post['creds'] is empty for ng2 restful webservice:
let creds = "email=" + userDetails.email + "&password=" + userDetails.password;
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
this.http.post("http://www.dr-gontar.com/checkSignIn/", creds, options)
.subscribe((data:Response) => this.id = JSON.parse(data.text()).id);
this.http.post("http://www.dr-gontar.com/checkSignIn/", creds, options )
.subscribe((data:Response) => this.email = JSON.parse(data.text()).email);
this.http.post("http://www.dr-gontar.com/checkSignIn/", creds, options )
.subscribe((data:Response) => this.name = JSON.parse(data.text()).name);
this.http.post("http://www.dr-gontar.com/checkSignIn/", creds, options)
.subscribe((data:Response) => this.mobile = JSON.parse(data.text()).mobile);
this.http.post("http://www.dr-gontar.com/checkSignIn/", creds, options )
.subscribe((data:Response) => this.address = JSON.parse(data.text()).address);
this.userDetails = [this.id,this.email,this.name,this.mobile,this.address];
console.log(this.userDetails);
Use URLSearchParams:
sendUserAndPass(userDetails) {
let body = new URLSearchParams();
body.set('email', userDetails.email);
body.set('password', userDetails.password)
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
this.http.post('url', body.toString(), options)
.map(res => res.json())
.subscribe(data => console.log(data))
}
Then you can e.g get your e-mail like:
$_POST['email']