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

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.

Related

my flutter app is unable to get or post https request

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

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
});

I have api in ajax format how can i code in android to get the response

I have api in ajax format ,how can i code in android to get access to server.I have API ID and API key and URL for access to server.
My api code is in the format
$.ajax({
url: 'https://www.abc.com/server/api/user.login'
dataType: 'json',
method: 'POST',
data: {
api_id: 4,
api_key: '06875425-b293-43af-9966-1245125f6bb95b1f7c5e-f932-4916-87f5-db0b298f2',
token: 'api_token_from_previous_call',
},
success: function(response){
console.log(response);
}
});
The code you are showing is jQuery. Are you running that code from within a webview in your Android app or something?
Here are some quick thoughts:
If you need to hit your API from native Android code, I recommend Google's Volley library for making the network request. You can read about Volley here, and this video from last year's Google I/O is a good intro to what it can do.
Once you get the response, you can use the Android JSONObject class to work with it.

Categories

Resources