I’m struggling to update some old React Native code that uses an AI image colorizer API. The API now requires image uploads instead of base64 encoding. I’m using react-native-imagepicker to select images but can’t figure out how to send them to the API correctly.
Here’s what I’ve tried:
import React from 'react';
import { View, Alert } from 'react-native';
import { Button } from 'react-native-paper';
import { launchImageLibrary } from 'react-native-image-picker';
import axios from 'axios';
const ImageColorizer = () => {
const pickImage = async () => {
const result = await launchImageLibrary({ mediaType: 'photo' });
if (result.assets) {
const imageUri = result.assets[0].uri;
colorizeImage(imageUri);
}
};
const colorizeImage = async (imageUri) => {
const formData = new FormData();
formData.append('image', { uri: imageUri, type: 'image/jpeg', name: 'photo.jpg' });
formData.append('renderFactor', '20');
try {
const response = await axios.post(
'https://example-colorizer-api.com/colorize',
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
'X-API-Key': 'your-api-key-here'
}
}
);
Alert.alert('Success', 'Image colorized!');
} catch (error) {
Alert.alert('Error', 'Failed to colorize image');
}
};
return (
<View>
<Button onPress={pickImage}>Pick and Colorize Image</Button>
</View>
);
};
export default ImageColorizer;
This code compiles but I get a server error when trying to colorize an image. Am I formatting the image data correctly? Any tips on debugging API issues in React Native?