formData problems using fetch to attempt image upload - react-native

I am attempting to upload an image from React Native to a server. I have used a 'picker' (react-native-document-picker) to select the image and that part seems to be functional. Here is the code I am working with:
import React, { Component } from "react";
import { Button, Dimensions, Image, Platform, SafeAreaView, ScrollView, StyleSheet, Text, TextInput, TouchableOpacity, View } from "react-native";
import DocumentPicker from 'react-native-document-picker';
const SERVER_URL = 'http://localhost:3000';
export default class Images extends Component {
constructor(props) {
super(props);
this.state = {
photo: null,
};
}
ChoosePhoto = async () => {
try {
const res = await DocumentPicker.pickSingle({ type: [DocumentPicker.types.images], });
this.setState({ photo: res }); //Set the state to the selected file attributes
} catch (err) {
this.setState({ photo: null });
if (DocumentPicker.isCancel(err)) {
alert('Canceled'); //user canceled the document selection
} else {
alert('Selection Error: ' + JSON.stringify(err)); //Unknown Error
} //Handling any exception (if any)
} //try routine
};
UploadPhoto = async () => {
if (this.state.photo != null) {
//if file selected then create FormData
const fileToUpload = this.state.photo.uri;
const data = new FormData();
data.append('name', 'Image Upload');
data.append('type', this.state.photo.type);
data.append('file_attachment', fileToUpload);
console.log("data.name is: " + data.name); //"undefined"
console.log("data.type is: " + data.type); //"undefined"
console.log("data.file_attachment is: " + data.file_attachment); //"undefined"
console.log("fileToUpload is: " + fileToUpload); //correct
//****UPLOAD TO SERVER
fetch(`${SERVER_URL}/api/upload`, {
method: 'POST',
body: data,
headers: { 'Content-Type': 'multipart/form-data' }
})
.then((response) => response.json())
.then((response) => {
console.log('Upload success: ', response);
alert('Upload success!');
this.setState({ photo: null });
})
.catch((error) => {
this.setState({ photo: null });
this.setState({ notify: "Pending Upload..." });
console.log('Upload error: ', error);
alert('Upload failed!');
});
//****
} else {
alert('Please Select File to Upload...');
} //'this.state.photo' is NOT NULL...OR NOT...
};
...
} //class 'Images' Component
As can be seen in my comments, the 'console.log' statements following the definition of the FormData and the '.append' statements are all "undefined" (in the 'UploadPhoto' function)...even the 'name' which was set to a simple string ('Image Upload') is not defined. So obviously the entire FormData command is totally non-functional.
I have spent days trying to figure this...difficult to believe in 2023 something like this should be so convoluted. I have previously had little difficulty uploading images from web applications before however this is my first attempt at using 'FormData' and attempting a file upload from React Native.
Any advice is greatly appreciated. I thank you in advance.

To access values in FormData use get.
const data = new FormData();
data.append('name', 'Image Upload');
console.log("data.name is: " + data.get("name"));
To upload a file
const body = new FormData()
body.append('file', {uri: 'path/to/content', type: 'type', name: 'name'})
const response = await fetch('url', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: body
})

Related

React Native Image Upload with FormData to NestJS server using a FileInterceptor, but 'file' is undefined

My React Native application receives a selected image using the react-native-image-picker library and I need to send that image to a back-end running a NestJS server. The endpoint uses #UseInterceptor and FileInterceptor to extract the image from the 'file' field of the formData received. However, when I fire the request to the endpoint, the file received is undefined.
Here is my React Native code sending the request with the file in the FormData as a payload.
const uploadNewProfileImage = async () => {
if (!newProfileImage?.assets) return;
const formData = new FormData();
const profileImage = newProfileImage.assets[0];
console.log(profileImage);
if (profileImage.uri && user) {
formData.append(
'file',
JSON.stringify({
uri:
Platform.OS === 'android'
? profileImage.uri
: profileImage.uri.replace('file://', ''),
name: profileImage.fileName,
type: profileImage.type
})
);
client // client is an Axios instance that injects Bearer Token
.post(`/user/profile/${user.uid}/image`, formData)
.then(({ data }) => {
console.log(data);
})
.catch((err) => {
console.log(err.response);
setShowImageUploadError(true);
})
.finally(() => {
getUserProfile();
});
}
};
Here is my back-end NestJS code extracting the file.
// User.controller.ts
#UseGuards(UserGuard)
#ApiBearerAuth()
#ApiUnauthorizedResponse({ description: 'Unauthorized' })
#UseInterceptors(FileInterceptor('file', { limits: { fileSize: 20000000 } }))
#Post('/profile/:uid/image')
#ApiOkResponse({ type: UploadProfileResponse })
#ApiBadRequestResponse({ description: 'Image too large OR Invalid image type' })
async uploadProfilePicture(#UploadedFile() file: Express.Multer.File, #Request() req): Promise<UploadProfileResponse> {
const uid = req.user.uid;
const imageUrl = await this.userService.uploadProfilePicture(uid, file);
return imageUrl;
}
}
I tried to set the axios request header in the axios config like so
{
headers: {
'Content-Type': 'multipart/form-data; boundary=——file'
}
}
I tried chaning the back-end endpoint to the following
#UseGuards(UserGuard)
#ApiBearerAuth()
#ApiUnauthorizedResponse({ description: 'Unauthorized' })
#UseInterceptors(FileFieldsInterceptor([{ name: 'file' }], { limits: { fileSize: 20000000 } }))
#Post('/profile/:uid/image')
#ApiOkResponse({ type: UploadProfileResponse })
#ApiBadRequestResponse({ description: 'Image too large OR Invalid image type' })
async uploadProfilePicture(#UploadedFiles() file: Array<Express.Multer.File>, #Request() req): Promise<UploadProfileResponse> {
const uid = req.user.uid;
console.log("File", file);
const imageUrl = await this.userService.uploadProfilePicture(uid, file[0]);
return imageUrl;
}
Nothing seems to be working, and the file extracted from the backend is still undefined.
Any help would be greatly appreciated.

React redux - passing parameters to url - error - Actions must be plain objects

I want to attach params to react redux fetch action and I searched for many days the redux docs, but even after trying out a few things i am getting this error:
[Unhandled promise rejection: Error: Actions must be plain objects. Use custom middleware for async actions.]
https://codesandbox.io/s/fast-framework-ct2fc?fontsize=14&hidenavigation=1&theme=dark
The original action looks like this:
export function fetchArticleDetails() {
return apiAction({
url: "http://myurl/appApi/2.0.0/getData/1", //1 should be an optional value
onSuccess: setArticleDetails,
onFailure: () => console.log("Error occured loading articles"),
label: FETCH_ARTICLE_DETAILS
});
}
function setArticleDetails(data) {
console.log(data);
return dispatch({
type: SET_ARTICLE_DETAILS,
payload: data
});
}
i tried to set the param directly
export function fetchArticleDetails(id)
...
url: `http://myurl/appApi/2.0.0/getData/${id}`,
or some variations to put the params in the payload directly
function setArticleDetails(data) {
console.log(data);
return dispatch({
type: SET_ARTICLE_DETAILS,
payload: data,
userid: id
});
}
All this results in the same error. Anyone have an idea where to place the dynamic data to solve it?
Another idea could be to set the params in my reducer maybe?
Update store/index.js
import { createStore, applyMiddleware } from "redux";
import rootReducer from "../reducers";
import apiMiddleware from "../middleware/api";
const store = createStore(rootReducer, applyMiddleware(apiMiddleware));
window.store = store;
export default store;
update: middleware/api.js
import axios from "axios";
import { API } from "../actions/types";
import { accessDenied, apiError, apiStart, apiEnd } from "../actions/api";
const apiMiddleware = ({ dispatch }) => next => action => {
next(action);
if (action.type !== API) return;
const {
url,
method,
data,
accessToken,
onSuccess,
onFailure,
label,
headers
} = action.payload;
const dataOrParams = ["GET", "DELETE"].includes(method) ? "params" : "data";
// axios default configs
axios.defaults.baseURL = process.env.REACT_APP_BASE_URL || "";
axios.defaults.headers.common["Content-Type"] = "application/json";
axios.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`;
if (label) {
dispatch(apiStart(label));
}
axios
.request({
url,
method,
headers,
[dataOrParams]: data
})
.then(({ data }) => {
dispatch(onSuccess(data));
})
.catch(error => {
dispatch(apiError(error));
dispatch(onFailure(error));
if (error.response && error.response.status === 403) {
dispatch(accessDenied(window.location.pathname));
}
})
.finally(() => {
if (label) {
dispatch(apiEnd(label));
}
});
};
export default apiMiddleware;
function apiAction()
function apiAction({
url = "",
method = "GET",
data = null,
accessToken = null,
onSuccess = () => {},
onFailure = () => {},
label = "",
headersOverride = null
}) {
return {
type: API,
payload: {
url,
method,
data,
accessToken,
onSuccess,
onFailure,
label,
headersOverride
}
};
}
There are a couple of issues with the code. apiMiddleware should only pass the action to the next middleware in the chain if it's not of type API.
const apiMiddleware = ({ dispatch }) => (next) => (action) => {
if (action.type !== API) {
return next(action)
}
// do stuff
}
Since the apiMiddleware dispatches what onFailure returns, the function has to return an object. In fetchArticleDetails, you're passing () => console.log("Error occured loading articles") causing apiMiddleware to dispatch undefined.
export function fetchArticleDetails(id) {
return apiAction({
url: `https://jsonplaceholder.typicode.com/todos/${id}`,
onSuccess: setArticleDetails,
onFailure: (error) => ({
type: FETCH_ARTICLE_ERROR,
payload: error
}),
label: FETCH_ARTICLE_DETAILS
})
}
CodeSandbox
I would strongly recommend using React Query to simplify data fetching, managing, and syncing server state.

react native fetch hook and refresh jwt token

i have made a custom hook to fetch data from api, but the token only valid for 5 second.
so i made this hook
the problem is when i call the hooks from my page it called many time and the refresh token already expired
when i access the api i will check the response first if the token invalid i tried to refresh my token using handleRefreshToken
nb : im using useContext for my state management
import React, {useEffect, useState, useContext} from 'react';
import {View, StyleSheet} from 'react-native';
import {AuthContext} from '../Auth/Context';
import AsyncStorage from '#react-native-community/async-storage';
import {urlLogin, URLREFRESHTOKEN} from '../Configs/GlobaUrl';
const FetchData = () => {
const {loginState, authContext} = useContext(AuthContext);
const [data, setData] = useState([]);
const [message, setMessage] = useState('');
const [loading, setIsLoading] = useState(false);
const {dispatchRefreshToken} = authContext;
const handleRefreshToken = async (callbackUrl, callbackBody) => {
const refBody = {
client_id: loginState.ipAddress,
ipAddress: loginState.ipAddress,
employee_id: loginState.userData.Pegawai_Id,
jwttoken: loginState.userToken,
refresh_tokenn: loginState.refreshToken,
};
console.log('======refreshtokencalled==========');
console.log(refBody.refresh_tokenn, '<=refresh token');
console.log(refBody.jwttoken, '<=jwt token');
let response = await fetch(URLREFRESHTOKEN, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(refBody),
redirect: 'follow',
});
let result = await response.json();
console.log(result, ' ini result');
if (
result.item3 !== 'refresh token gagal' &&
result.item3 !== 'refresh token sudah tidak berlaku'
) {
let refresh = result.item2;
let token = result.item1;
// the backend doesnt send any succes / error code only item1 for token, //item2 refresh token and item3 for error
dispatchRefreshToken(token, refresh);
await AsyncStorage.setItem('refreshToken', refresh);
await AsyncStorage.setItem('token', token);
return getData(callbackUrl, callbackBody);
} else {
return null;
}
};
const getData = async (url, body) => {
setIsLoading(true);
let result;
try {
let response = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${loginState.userToken}`,
},
body: JSON.stringify(body),
redirect: 'follow',
});
if (response.status == '401') {
let refreshResult = await handleRefreshToken(url, body);
console.log(refreshResult);
} else {
result = await response.json();
console.log(result);
console.log(loginState.refreshToken);
if (result.code == '1') {
setData(result.data);
setIsLoading(false);
} else {
throw result;
}
}
} catch (err) {
setData([]);
console.log(err, 'masuk error usefetchbybutton');
console.log(err.message, err.code);
setIsLoading(false);
setMessage(err);
}
};
return {
data: data,
message: message,
loading: loading,
getData: getData,
};
};
export default FetchData;
this is my dispatch refresh token
const authContext = useMemo(
() => ({
logIn: async (token, userData, refreshToken) => {
console.log(token, '<>', refreshToken, 'ini memoisa');
dispatch({
type: 'LOGIN',
token: token,
userData: userData,
refreshToken: refreshToken,
});
},
logOut: () => {
AsyncStorage.clear((error) => {
console.log(error);
});
dispatch({type: 'LOGOUT'});
},
dispatchRefreshToken: (userToken, refreshToken) => {
console.log(refreshToken, '=refresh dispatch=');
console.log(userToken, '=userToken dispatch=');
dispatch({
type: 'REFRESHTOKEN',
userToken: userToken,
refreshToken: refreshToken,
});
},
}),
[],
);
my reducer function
const loginReducer = (prevState, action) => {
switch (action.type) {
some case ...
case 'REFRESHTOKEN':
return {
...prevState,
userToken: action.userToken,
refreshToken: action.refreshToken,
};
}
};
Use recursion. The pseudo code is as follows
const getData = async (args, times) => {
// try to fetch data
const data = await Api.fetch(args);
// if token need to be refreshed.
if (check401(data)) {
// Use variable times to prevent stack overflow.
if (times > 0) {
// refresh the token
await refreshToken()
// try again
return getData(args, times - 1);
} else {
throw new Error("The appropriate error message")
}
}
return dealWith(data)
}
The logical above can be encapsulated to all your api. Like this
const wrapApi = (api) => {
const wrappedApi = async (args, times) => {
const data = await api(args);
// if token need to be refreshed.
if (check401(data)) {
// Use variable times to prevent stack overflow.
if (times > 0) {
// refresh the token
await refreshToken()
// try again
return wrappedApi(args, times - 1);
} else {
throw new Error("The appropriate error message")
}
}
return dealWith(data)
}
return wrappedApi;
}

React Native Request failed with status code 400

Does anyone have experience working with this API:
https://authenticjobs.com/api/docs#introduction
I have been running through a few iterations of implementation in my React Native project and I was originally getting this error:
undefined is not an object (evaluating '_ref.longitude')
And now I am getting this error:
Request failed with status code 400
This is the action creator that is attempting to log the data object of this API request.
import axios from "axios";
import reverseGeoCode from "latlng-to-zip";
import qs from "qs";
import { FETCH_JOBS } from "./types";
const JOB_ROOT_URL = "https://authenticjobs.com/api/?";
const JOB_QUERY_PARAMS = {
api_key: "5634cc46389d0d872723b8c46fba672c",
format: "json"
// latlong: 1,
// radius: 10,
// q: "javascript"
};
const buildJobsUrl = zip => {
const query = qs.stringify({ ...JOB_QUERY_PARAMS, l: zip });
return `${JOB_ROOT_URL}${query}`;
};
export const fetchJobs = region => async dispatch => {
try {
let zip = await reverseGeoCode(region);
const url = buildJobsUrl(zip);
let { data } = await axios.get(url);
dispatch({ type: FETCH_JOBS, payload: data });
console.log(data);
} catch (e) {
console.log(e);
}
};
I got it to work with this refactor, although this is going to kind of take my application in a different direction:
import axios from "axios";
import { FETCH_JOBS } from "./types";
const JOB_ROOT_URL = "https://authenticjobs.com/api/?api_key=";
const JOB_QUERY_PARAMS = {
key: "a446a0eefe6f5699283g34f4d5b51fa0",
method: "aj.jobs.getLocations",
format: "json",
category: "javascript"
};
export const fetchJobs = region => async dispatch => {
try {
const url =
JOB_ROOT_URL +
JOB_QUERY_PARAMS.key +
"&method=" +
JOB_QUERY_PARAMS.method +
"&category=" +
JOB_QUERY_PARAMS.category +
"&format=" +
JOB_QUERY_PARAMS.format;
let { data } = await axios.get(url);
dispatch({ type: FETCH_JOBS, payload: data });
console.log(data);
} catch (e) {
console.log(e);
}
};

get item from asyncstorage in constructor and pass it to other method - react native

I have a file called Server.js in which I have written parent method to make get and post request. I am trying to extract few items from async-storage in the constructor and passing it to a method where I am assigning it to another variable. Now I have to use this variable inside my get and post method but unable to access the new variables(which are declared inside the constructor itself).
These new initialized variables are accessible by post method, but not inside the get method. When I tried some other workaround, it says that all my 3 variables ( participantId, mobeToken, urlAsync are undefined. Any input on my above issue.
variables :
1. participantId: will be sent as a header attribute (for authentication).
2. mobeToken: token for authentication, sent in Get request just like participantId.
3. urlAsync: its the url in which all the calls will be made. Once this is accessible inside Get and post method, I will remove this.baseUrl from both, Get & Post method request.
import React, { Component } from "react";
import { Alert,AsyncStorage} from 'react-native';
import axios from "axios";
const DEV_ENV_URL = "http://www.aaa.com/api";
const STAGE_ENV_URL = "http://www.bbb.com/api";
const PROD_ENV_URL = "http://www.ccc.com/api";
export default class Server extends Component {
baseUrl = DEV_ENV_URL;
constructor(props) {
super(props);
mobeToken1 = "";
participantId1 = "";
urlAsync = "";
AsyncStorage.getItem("profile", (err, res) => {
if (res !== null) {
let profile = JSON.parse(res);
participantId = profile.participantId;
} else {
profile = "";
}
AsyncStorage.getItem("mobeToken", (err, mobeToken) => {
AsyncStorage.getItem("URL", (err, Url) => {
this.assignValues(participantId, mobeToken, Url);
});
});
});
}
assignValues(participantId, mobeToken, Url) {
participantId1 = participantId;
mobeToken1 = mobeToken;
urlAsync = Url;
console.log("mobeToken1 "+ mobeToken1 + "," + participantId1 +"," + urlAsync);
}
getRequest(url) {
// debugger;
console.log(mobeToken1); // need variables in this method but can't
return fetch(this.baseUrl + url , {
method: 'GET',
headers: {
}
})
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
return responseJson;
})
.catch(error => {
console.log(error);
Alert.alert("", "Network connection issue. Please contact support");
});
}
postRequest(url, jsonData) {
return fetch(this.baseUrl + url, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
// 'Content-Type': 'application/text',
"parse-array": "|entryJSONList|",
"mobeToken": mobeToken1,
"participantId": participantId1,
},
body: JSON.stringify(jsonData)
})
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
return responseJson;
})
.catch(error => {
console.log(error);
Alert.alert("", "Network connection issue. Please contact support");
});
}
}
Is there any alternative solution for this?
Thanks for viewing or providing solutions to this....