Search results
Top results related to react native axios js connections
Top Answer
Answered Nov 19, 2019 · 3 votes
I got the answer for my question
React native code :
- upload_File() { if (this.validate_Fields()) { const { image, images, files, description, user_id } = this.state; // this.setState({ error: '', loading: true }); if (this.state.type === 'image/jpeg') { console.log('upload_ files :::=> ', files); const formData = new FormData(); formData.append('user_id', user_id); formData.append('description', description); // formData.append('files[]', files); for (let i = 0; i < files.length; i++) { formData.append('files[]', { name: files[i].path.split('/').pop(), type: files[i].mime, uri: Platform.OS === 'android' ? files[i].path : files[i].path.replace('file://', ''), }); } axios.post(API_URL + '/fileuploadapi/uploadPost', formData, { headers: { "Content-type": "multipart/form-data" } }).then((response) => { console.log(JSON.parse(JSON.stringify(response.status))); this.cleanupImages(); Alert.alert('Upload Post Successfully ' + ''); }).catch((error) => { console.log(error); Alert.alert('image Upload Post Failed , Try again !'); }); } if (this.state.type === 'video/mp4') { console.log('upload_ files :::=> ', this.state.files); const formData = new FormData(); formData.append('user_id', user_id); formData.append('description', description); formData.append('files[]', { name: this.state.fileName, type: this.state.type, uri: Platform.OS === 'android' ? files.path : files.path.replace('file://', ''), }); axios.post(API_URL + '/fileuploadapi/uploadPost', formData, { headers: { "Content-type": "multipart/form-data" } }).then((response) => { console.log(JSON.parse(JSON.stringify(response.status))); this.cleanupImages(); Alert.alert('video Upload Post Successfully ' + ''); }).catch((error) => { console.log(error); Alert.alert('video Upload Post Failed , Try again !'); }); } if (this.state.type === "image/cam") { console.log('upload_ files :::=> ', this.state.files); const formData = new FormData(); formData.append('user_id', user_id); formData.append('description', description); formData.append('files[]', { name: this.state.fileName, type: this.state.type, uri: Platform.OS === 'android' ? files.path : files.path.replace('file://', ''), }); axios.post(API_URL + '/fileuploadapi/uploadPost', formData, { headers: { "Content-type": "multipart/form-data" } }).then((response) => { console.log(JSON.parse(JSON.stringify(response.status))); this.cleanupImages(); Alert.alert('camera Upload Post Successfully ' + ''); }).catch((error) => { console.log(error); Alert.alert('camera Upload Post Failed , Try again !'); }); } } }renderVideo(image) { console.log('rendering video'); return (<View style={{ height: 300, width: '100%' }}> <Video source={{ uri: this.state.files.path }} style={{ width: '100%', height: 300 }} resizeMode="cover" paused={this.state.paused} controls={true} volume={this.state.volume} muted={this.state.muted} paused={this.state.paused} onLoad={this.onLoad} onBuffer={this.onBuffer} onProgress={this.onProgress} /> </View>); } renderImage(image) { return <Image style={{ width: '100%', height: 500, resizeMode: 'cover', marginBottom: 6, borderRadius: 2, borderColor: 'green', borderWidth: 1, }} source={image} /> } renderAsset(image) { if (image.mime && image.mime.toLowerCase().indexOf('video/') !== -1) { return this.renderVideo(image); } return this.renderImage(image); } pickMultipleImages() { ImagePicker.openPicker({ multiple: true, mediaType: "photo", // waitAnimationEnd: false, // includeExif: true, // forceJpg: true, }).then(images => { // console.log('received images', images); console.log('received images mime:=>', images.mime); this.setState({ type: "image/jpeg", image: null, files: images, images: images.map(i => { // console.log('received image', i); console.log('received images mime::::', i.mime); return { uri: i.path, width: i.width, height: i.height, mime: i.mime }; }) }); }).catch(error => { console.log(error); Alert.alert(error.message ? error.message : error); }); } pickSingleVideo() { ImagePicker.openPicker({ mediaType: "video", // compressVideoPreset: 'MediumQuality', // includeExif: true, }).then(image => { console.log('received image', image); console.log('received image mime', image.mime); console.log('received file-memetype:', image.path.split('.').reverse()[0]) console.log('received fileName:', image.path.split('/').pop()) this.setState({ images: null, files: image, type: image.mime, fileName: image.path.split('/').pop(), image: { uri: image.path, width: image.width, height: image.height, mime: image.mime }, }); }).catch(error => { console.log(error); Alert.alert(error.message ? error.message : error); }); } pickSingleWithCamera() { ImagePicker.openCamera({ // cropping: cropping, // cropping: true, // width: 600, // height: 500, // includeExif: true, // mediaType: 'video', }).then(image => { console.log('received image', image); console.log('received image mime: ', image.mime); this.setState({ images: null, files: image, type: "image/cam", // type: "video/mp4", fileName: image.path.split('/').pop(), image: { uri: image.path, width: image.width, height: image.height, mime: image.mime }, }); }).catch(error => { console.log(error); Alert.alert(error.message ? error.message : error); }); } // clear files data cleanupImages() { this.setState({ // description: '', image: null, images: null, // video: '', files: '', }) ImagePicker.clean().then(() => { console.log('removed tmp images from tmp directory'); }).catch(error => { alert(error); }); }<View style={styles.mediaComponent}> <ScrollView> {this.state.image ? this.renderAsset(this.state.image) : null} {this.state.images ? this.state.images.map(i => <View key={i.uri}>{this.renderAsset(i)}</View>) : null} </ScrollView></View>```````````````````
1/5
Top Answer
Answered Sep 09, 2020 · 1 votes
Javascript promises can either be resolved (i.e. the requested resource was successfully retrieved) and rejected(i.e. the requested resource could not be retrieved).
The then() function only executes if the promise is resolved. Under the hood, if axios receives a response with an error code(Anything other than 2XX), then the promise will be rejected. Axios
In the case that a promise is rejected, you need to use the catch() callback. If you want to print the error, it should look something like the following:
axios.post(`http://localhost:3002/api/user/login`, { user }) .then(response => { console.log(response) console.log(response.data) }) .catch(error => console.log(error))
Essentially, .then() is not being executed because the request was unsuccessful, and a response other than 200/201/2XX was returned.
In your case, the issue is that you are passing {user}, where user is an object, so you are nesting the object. Remove the curly braces around user.
axios.post(`http://localhost:3002/api/user/login`, user)
You can read more about javascript promises, .then(), .catch() and .finally() here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
2/5
Top Answer
Answered Jan 16, 2020 · 0 votes
You may try below :
NPM : react-native-ssl-pinning
for more info : npmjs.com/package/react-native-ssl-pinning
3/5
Axios is a widely used HTTP client for making REST API calls. You can use this in React Native to get data from any REST API.
Features of Axios:
- It can make both XMLHttpRequests and HTTP requests.
- It can understand all the requests and responses from an API.
- Axios is promise-based.
- It can transform the response in JSON format.
How to install Axios in React Native:
You can install Axios in your React Native project with either npm or yarn. Open the terminal on your computer and go to your project directory.
- Using npm:
npm install axios
- Using yarn:
yarn add axios
You can make both GET and POST requests with Axios in React Native:
- The GET request is used to get data from an API.
- The POST request is used to modify the data on the API server.
GET: axios.get() method is used to perform GET requests in Axios with React Native. It takes a base URL to get data. You can specify parameters that you want to pass with the base URL with params.
If it gets executes successfully, you will get a response. This response will contain data and other information regarding the request. If any error occurs, then the catch statement will get that error.
If you want something to execute every time, in that case, you can write that under the statement.
Syntax:
axios.get('/GeeksforGeeks', { params: { articleID: articleID }}) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }).then function...
4/5
Top Answer
Answered Mar 04, 2022 · 8 votes
Axios' FormData is broken since v0.25 for react-native. They tried to optimize the FormData detection util for no real reason. Unfortunately, the proper detection fails for non browser environments.
You can find a workaround here for now: https://github.com/axios/axios/issues/4406#issuecomment-1048693170
TL;DR
const formData = new FormData();...const response = await axios.post('/URL', formData, { headers: { 'Content-Type': 'multipart/form-data', }, transformRequest: (data, headers) => { return formData; // this is doing the trick },});
Just pass a transformRequest and return your formData and everything will "work" magically again. This is a workaround though (I've spent 5 hours on this issue) and should hopefully be fixed in an upcoming version.
5/5
blog.logrocket.com › using-axios-with-react-nativeUsing Axios with React Native to manage API requests
blog.logrocket.com › using-axios-with-react-nativeMay 19, 2023 · Axios is also one of the easiest HTTP clients to learn and use. Making an API request is as simple as passing a configuration object to Axios or invoking the appropriate method with the necessary arguments.We will use a simple React Native application set up using Expo’s managed workflow for this tutorial.
stackoverflow.com › questions › 49370747Network error with axios and react native - Stack Overflow
stackoverflow.com › questions › 49370747If both your React Native application and your service are on the same local network, you can access your service using your local network IP. 1. Find Your Local IP Address: On your computer where the service is running, open a terminal and run the following command: ifconfig
Usage example
http://localhost:3000People also ask
Can Axios be used in React Native?
- Since React Native is built on Nodejs, Axios can also be employed in React Native applications. For more information about Axios, you can refer to the official documentation link. Using Axios is incredibly straightforward. Simply import Axios and use one of the following methods: GET, POST, PUT, or DELETE.
Making HTTP API calls with Axios in React Native
reactnativespace.com/tutorials/making-http-api-calls-with-axios-in-react-nativeHow to manage API requests using Axios in a React Native application?
- Axios will merge the configuration object passed while creating the instance with the configuration passed to the instance method: In this section, you will learn to manage API requests using Axios in a React Native application. You will use Axios to perform a simple CRUD (Create, Read, Update, and Delete) operation.
Using Axios with React Native to manage API requests
blog.logrocket.com/using-axios-with-react-native-manage-api-requests/Can Axios abort a network request in React Native?
- Axios provides functionality for aborting network requests. A typical use case of this feature in React Native is the cancellation of network requests in the useEffect Hook when a component is unmounted while data is still in flight. You can read the code snippet below to understand how to use this functionality:
Using Axios with React Native to manage API requests
blog.logrocket.com/using-axios-with-react-native-manage-api-requests/How do I access my React Native service?
- If both your React Native application and your service are on the same local network, you can access your service using your local network IP. 1. Find Your Local IP Address: ifconfig Look for your network interface (usually eth0 or en0) and find the inet address. It will be something like 192.168.x.x or 10.x.x.x. This is your local IP address. 2.
Network error with axios and react native - Stack Overflow
stackoverflow.com/questions/49370747/network-error-with-axios-and-react-nativemedium.com › @devnexPro › mastering-api-calls-inMastering API Calls in React Native: A Comprehensive Guide ...
medium.com › @devnexPro › mastering-api-calls-inAug 5, 2023 · In conclusion, API integration is a vital skill for React Native developers. We explored both Fetch and Axios methods for making API calls, covering GET, POST, PUT, and PATCH requests.
axios-http.com › docs › introGetting Started | Axios Docs
axios-http.com › docs › introWhat is Axios? Axios is a promise-based HTTP Client for node.js and the browser. It is isomorphic (= it can run in the browser and nodejs with the same codebase). On the server-side it uses the native node.js http module, while on the client (browser) it uses XMLHttpRequests.
reactnativespace.com › tutorials › making-http-apiMaking HTTP API calls with Axios in React Native
reactnativespace.com › tutorials › making-http-apiAug 5, 2023 · Axios is a powerful promise-based HTTP Client used in Nodejs and browser environments. Since React Native is built on Nodejs, Axios can also be employed in React Native applications. For more information about Axios, you can refer to the official documentation link. Using Axios is incredibly straightforward.
rapidapi.com › guides › axios-react-nativeHow to Use Axios in React Native - Rapid
rapidapi.com › guides › axios-react-nativeApr 14, 2023 · By using Axios in React Native, you can easily send and receive data from a server, making it an essential tool for any mobile app developer. Getting started. We'll use the [Expo CLI](Installation - Expo Documentation) to set up our React Native project. Expo CLI is a command-line interface that allows developers to easily create, develop, and ...
Searches related to react native axios js connections