1

I made a Laravel backend to receive image from React-Native app. I tested on postman and it is working well. But I am not sure how do I write RN code with the postman code.

enter image description here

How to upload image in React Native? I used "react-native-image-crop-picker".

1 Answer 1

2

I found answer.

try{
    let uploadData = new FormData();
    let myHeaders = new Headers();
    myHeaders.append("Content-Type", "multipart/form-data");
    myHeaders.append("gb-auth-token", myToken);

    uploadData.append('profile_img', {
        type: 'image/jpeg', 
        uri: profileImage,     // e.g: file:///storage/......./blabla.jpg
        name: 'upload.jpg'
    })
    uploadData.append("customer_id", id);
    const response = await fetch(CUSTOMER_PP, {
        method: 'POST',
        headers: myHeaders,
        body: uploadData
    })
    const data = await response.json();
    return data;
} catch (err) {
    console.log(err);
}

In this code, this is core.

    uploadData.append('profile_img', {
        type: 'image/jpeg', 
        uri: profileImage,     // This value comes from "react-native-image-crop-picker" response.path
        name: 'upload.jpg'
    })
Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad my answer was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.