I have used Axios for API call in react native but get result Network Error.
const config = {
method: 'post',
url: BASE_URL+"/auth/login",
headers: {
'Content-Type': 'application/json',
},
data: {
username: "physician#doral.com",
password: "password",
device_token: "",
device_type: 1,
latitude: "",
longitude: ""
}
};
axios(config)
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log("err :", err);
Alert.alert("something went to wrong.",err.message);
});
I had use above axios function but get network error in react native. how to fix network error in react native Application.
Related
I am new in React Native Development. I had call Login Api it's every time give me Network error. How to fixed it? Please help me below is my code.
const config = {
method: 'post',
url: BASE_URL+"/auth/login",
headers: {
'Content-Type': 'application/json',
},
data: {
username: "physician#doral.com",
password: "password",
device_token: "",
device_type: 1,
latitude: "",
longitude: ""
}
};
axios(config)
.then((response) => {
console.log(response);
})
.catch((err) => {
console.log("err :", err);
Alert.alert("something went to wrong.",err.message);
});
I have try using fetch and Axios but get same result. please help me how to fixed and get proper response of this api.
I am using ApolloClient to send mutation that contains files (images) but I am getting
Error: Network request failed
this what I have done to create links
import { createUploadLink } from 'apollo-upload-client' v ==>> "^15.0.0";
const uploadLink = createUploadLink({
uri: API_URL,
headers: {
"Authorization": `Bearer ${token}`,
'Content-Type': 'multipart/form-data',
"Accept":"application/json"
},
});
this to handle errors
import { onError } from "#apollo/client/link/error"; v ==>> "^3.3.20"
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
if (networkError) console.log(`[Network zaid error]: ${networkError}`);
});
then :
const client = new ApolloClient({
cache: new InMemoryCache(),
link: from([errorLink,uploadLink]),
defaultOptions: {
watchQuery: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'none'
},
mutate: {
mutation: "DocumentNode",
errorPolicy: 'none'
},
},
});
then I called the mutation :
await client.mutate({
mutation:
gql`
mutation($data: PostCreatInput!, $files: [CustomCreateImages!]!) {
createpost(data: $data, files: $files) {
id
}}`,
variables: {
data: {****},
files:[{file:ReactNativeFile}]
}
}).then(response => {
console.log(response);
return response
}).catch(response => {
console.log(response);
return response
})
i used ReactNativeFile generated by apollo-upload-client
new ReactNativeFile({
uri: "file:///storage/***.jpg",
name: "a.jpg",
type: "image/jpeg"
});
I am using react native "react-native": "0.62.2"
and I have a live server not using localhost
I did check the server logs this request never left the mobile; there was no record of it on the server.
been stuck all day on it, any help would be highly appreciated!
bug with React Native 0.62+ that messes up the configuration for multiform requests. It can be fixed by commenting out line 43 in android/app/src/debug/java/com/maxyride/app/drivers/ReactNativeFlipper.java:
//builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
I'm trying to upload in react-native but have no luck after many tries.
Someone please help me out of this problem? Thank you verymuch
const handleChoosePicture = () => {
launchImageLibrary({ noData: true }, (response) => {
if (response) {
setChoosePicture(response);
}
});
}
const createFormData = (photo) => {
const data = new FormData();
data.append('files', {
name: photo.fileName,
type: photo.type,
uri: Platform.OS === 'ios' ? photo.uri.replace('file://', '') : photo.uri,
});
return data;
};
const uploadImage = async (choosePicture) => {
const Token = await AsyncStorage.getItem('member_token');
await axios.post(
`${AppConfig.apiUrl}/media/upload`,
createFormData(choosePicture),
{
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
authorization: `Bearer ${Token}`,
},
})
.then((response) => {
console.log('response', response);
})
.catch((error) => {
console.log('error', error);
});
}
Possible Unhandled Promise Rejection (id: 9):
TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[17], "axios").AxiosInstance.post')
I think you just need to restart the packager.
If you are running a metro-packager and install a new dependency, you need to restart the packager so it can add the new dependencies to the dependencyMap.
[UPDATE]
You can also check if some of your libs is incompatible with your react-native version. I've just talked with a friend of mine, and he was with the same problem as you (dependencyMap) but with custom component, the problem in fact was react-native-gesture-handler that was in an old version
TypeError: Network request failed - I get this error when I try to log in what is the solution to my problem here is the code that I use and thank you
constructor(props)
{
super(props)
this.state={email1:'',password1:''}
}
login = () =>
{
const {email1}=this.state;
const {password1}=this.state;
fetch('http://192.168.1.114',{
method: 'POST',
headers:{
'Accept': 'application/json',
'Content-Type':'application/json'
},
body: JSON.stringify({
email: 'email1',
password: 'password1'
})
})
.then((response)=>response.json())
.then((responseJson)=>{
alert(responseJson)
})
.catch((error)=>{
console.error(error);
});
I am trying to send blob images to the server but react native is showing the error network failed error.
get_data = async () => {
let file = new FormData();
fetch("https://jzzqf.sse.codesandbox.io/", {
method: "POST",
headers: {
"Content-Type": "multipart/form-data; charset=utf-8",
//"Content-Type": "text/html; charset=utf-8",
"Access-Control-Allow-Origin": "*"
},
mode: "no-cors",
body: file
})
.then(function(response) {
alert(response);
})
.catch(function(error) {
alert(error);
});
};
Switch your then to handle the promise rejection
.then(response =>
{
alert(response);
}, reject => {
// handle reject here
})