my flutter app is unable to get or post https request - android

I am building a flutter app, everything is alright if I use http requests but when it come to https, I am unable to get or post https request, debugger gives no error, even it does not print response of the request. I tried, flutter clean and rebuild app so many times even clearing my mobile device cache etc. it does not work
I am working and testing on android device
Code
import 'dart:convert';
import 'package:http/http.dart' as http;
var url = Uri.parse('https://my.api-end-point.com:port/auth');
Map requestHeaders = <String, String>{
'Content-type': 'application/json',
'Accept': 'application/json'
};
Map body = <String, String>{'email': email, "password": password};
var response = await http.post(url, headers: requestHeaders, body: json.encode(body));
var res = jsonDecode(response.body);
print(res);
** Test **
working also on postman
any help will be appreciated!

Have you tried hitting the same api using postman?
may be there is an issue with the api itself since the console returns no errors

Related

axios request with headers only working if react-native-debugger is enabled

I have this weird experience like if I enable network inspect in react native debugger then the Axios request sends response properly, but if I disable the network inspect then the response I get is wrong.
In detail: I have an API that returns the token expired status I have set the token to expire to 1 day so if the token expires I get '-1' if the token is valid I get '0'. But if I enable network inspect then the responses are correct, if I disable network inspect then it does not check the token for expiring like the API returns '0' for both correct and wrong token. This works fine in postman also.
see my API request below:
await axios({
method: 'POST',
url: `https://xxxxxxxxx.in/univadmin/app.phpa=uniliteGetMenus&univcode=${data.funivcode}`,
data: {
mobile: data.fmobileno,
imei: 'imei'
},
headers: {
'X-Auth-Origin': 'UXXXXXE',
'X-Auth-Token': token,
'Content-Type': 'application/json'
}
})
I was facing so many problem while using Axios in sending files.
i recommend using RNFetchblob

react native android: TypeError: Network request failed

I'm using fetch command, running on Android emulator. When I used it with port "127.0.0.1" I got this error all the time no matter what type of request I was doing.
Then I changed it to http://10.0.2.2, and GET requests generally work, but I'm trying to make a POST request and get this error:
TypeError: Network request failed
My code:
const url = 'http://10.0.2.2:143'
const payload = {
method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
['x-access-token']: `Bearer ${token}`,
},
body,
}
const response = await fetch(url, payload)
On iOS everything's working fine so I don't think it's a problem with this code. What can I try to make it work on Android
The request doesn't even reach the server
if you are using android 9 or higher you should make requests with https , http has security problems

React Native: Multipart body must have at least one part

I've seen "Multipart body must have at least one part" and I understand what it means, but I have no idea how to solve it.
We've got a React Native app, where it consumes a Swagger-autogenerated API client and at one point we make a POST request to an endpoint with no data. The endpoint does accept data though all the fields are optional and I should not send any data at that point.
On iOS, everything works perfect. On Android:
If I'm on React Native Debugger (standalone one) + Enable Network Inspect, the API request works perfectly.
If I'm on Vscode debugger, the request fails without even trying with the following error:
Multipart body must have at least one part.
Without being able to change the SDK (as it's autogenerated from Swagger, and the actual HTTP request is many middlewares deep), how can I get rid of this problem? It should be possible as the same HTTP request works on iOS.
Try to pass body as empty FormData in fetch function body parameter
See Example
var data = new FormData()
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: data
});

Why am I getting 403 Forbidden Request Error?

I'm trying to download a pdf file using Dart programming language and Flutter framework.
If I send a GET request with my Browser or Postman everything works fine. However when I try using:
await http.readBytes("https://www.nato.int/structur/recruit/documents/Close%20Protection%20Agent,%20Close%20Protection%20Unit.pdf")
I get Request to ... failed with status 403: Forbidden.
I'm using this import
import 'package:http/http.dart' as http;
Why requesting from browser works and dynamically I get this error. If my url is bad encoded how should I encode it ? Isn't it already properly encoded ?
Thanks in advance!
The issue seems to be caused by using http.readBytes(). What you can do here is wait for a response from http.post() and write the response to the device's storage.
final response = await http.post(uri);
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File file = File('$tempPath/yourFile.pdf');
await file.writeAsBytes(response.bodyBytes);

Fetch request giving HTML response only on Android in React Native app

I have a react native app and a normal fetch request for login is not behaving ideally for Android. I'm making a normal fetch request like this for Login--
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
}),
});
Now, this works totally fine on iOS and I get the JSON response which I'm supposed to. But on Android, I get an HTML response. And that HTML response is the web counterpart of the app. What could be the possible issues for this? since I'm told that issue is at my end and couldn't find anything. Also, I've checked it on Postman and it is working fine.

Categories

Resources