React Native Fetch give error “Network request failed”
I have the following code for creating an event with a image and some body params. It was working fine when i was doing it without image, i am using react-native-image-crop-picker
for selecting images. I am getting "Network request failed" error when posting data from react-native. The request never reach my backend as i am getting no logs there. It is working fine with postmen.
MY CODE:
const { name, date, description, location, uri, mime, time } = this.state;
const formData = new FormData();
formData.append('name', name)
formData.append('date', date)
formData.append('description', description)
formData.append('location', location)
formData.append('time', time)
formData.append('image',{
uri:uri,
mime:'image/jpeg',
name:`image${moment()}`
})
alert(JSON.stringify(formData));
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData,
};
fetch(`http://${Config.apihost}:${Config.port}/events`,config).then((res) => res.json())
.then((res) => {
this.setState({ modalVisible: false, name:'', date: moment().format('YYYY-MM-DD'), description:'', Location: 'AlHedaya Masjid' })
this.props.addEvent(res.message);
// this.props.navigation.goBack();
}).catch((err) => alert(err));
I have another screen which contains different number of pictures like gallery i am uploading multiple picture to the gallery, the request is working fine with code below.
const data = new FormData();
data.append('name', 'avatar');
images.map((res, i) => {
data.append('fileData', {
uri: res.path,
type: res.mime,
name: `image${i}${moment()}`
});
})
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: data,
};
fetch(`http://${Config.apihost}:${Config.port}/events/${this.state.item.id}/photos`, config)
.then((checkStatusAndGetJSONResponse) => checkStatusAndGetJSONResponse.json())
.then((response) => {
if (response.status && response.message.length > 0) {
var images = this.state.images;
response.message.map(file => {
images.push(`http:${Config.apihost}:${Config.port}/images/${file.id}`);
});
this.setState({ images });
}
}).catch((err) => { alert(err) });
I can't really see the difference between the two codes but the upper code giving me error.
- I am testing on android
- I am using the IP address instead of localhost (my others requests are working so thats out of equation)
- None of the solution in this link worked
React Native fetch() Network Request Failed
Am I missing something?
javascript reactjs react-native fetch
|
show 1 more comment
I have the following code for creating an event with a image and some body params. It was working fine when i was doing it without image, i am using react-native-image-crop-picker
for selecting images. I am getting "Network request failed" error when posting data from react-native. The request never reach my backend as i am getting no logs there. It is working fine with postmen.
MY CODE:
const { name, date, description, location, uri, mime, time } = this.state;
const formData = new FormData();
formData.append('name', name)
formData.append('date', date)
formData.append('description', description)
formData.append('location', location)
formData.append('time', time)
formData.append('image',{
uri:uri,
mime:'image/jpeg',
name:`image${moment()}`
})
alert(JSON.stringify(formData));
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData,
};
fetch(`http://${Config.apihost}:${Config.port}/events`,config).then((res) => res.json())
.then((res) => {
this.setState({ modalVisible: false, name:'', date: moment().format('YYYY-MM-DD'), description:'', Location: 'AlHedaya Masjid' })
this.props.addEvent(res.message);
// this.props.navigation.goBack();
}).catch((err) => alert(err));
I have another screen which contains different number of pictures like gallery i am uploading multiple picture to the gallery, the request is working fine with code below.
const data = new FormData();
data.append('name', 'avatar');
images.map((res, i) => {
data.append('fileData', {
uri: res.path,
type: res.mime,
name: `image${i}${moment()}`
});
})
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: data,
};
fetch(`http://${Config.apihost}:${Config.port}/events/${this.state.item.id}/photos`, config)
.then((checkStatusAndGetJSONResponse) => checkStatusAndGetJSONResponse.json())
.then((response) => {
if (response.status && response.message.length > 0) {
var images = this.state.images;
response.message.map(file => {
images.push(`http:${Config.apihost}:${Config.port}/images/${file.id}`);
});
this.setState({ images });
}
}).catch((err) => { alert(err) });
I can't really see the difference between the two codes but the upper code giving me error.
- I am testing on android
- I am using the IP address instead of localhost (my others requests are working so thats out of equation)
- None of the solution in this link worked
React Native fetch() Network Request Failed
Am I missing something?
javascript reactjs react-native fetch
Shouldimages.push("http:${Config.apihost}:${Config.port}/images/${file.id}");
beimages.push("http://${Config.apihost}:${Config.port}/images/${file.id}");
?
– Dacre Denny
Jan 2 at 21:41
Thats when i actually post successfully to my server .. i am not even getting to my server the request is being stuck somewhere in the middle.
– Ahsan Sohail
Jan 2 at 21:46
Nice pick btw but the below code is working fine .. and now i am even more confused that why it is working.
– Ahsan Sohail
Jan 2 at 21:48
Yep - just a comment as I was looking at your code as it might be a problem later on
– Dacre Denny
Jan 2 at 21:48
Are you using Android emulator or real device for testing ?
– Tarreq
Jan 2 at 22:19
|
show 1 more comment
I have the following code for creating an event with a image and some body params. It was working fine when i was doing it without image, i am using react-native-image-crop-picker
for selecting images. I am getting "Network request failed" error when posting data from react-native. The request never reach my backend as i am getting no logs there. It is working fine with postmen.
MY CODE:
const { name, date, description, location, uri, mime, time } = this.state;
const formData = new FormData();
formData.append('name', name)
formData.append('date', date)
formData.append('description', description)
formData.append('location', location)
formData.append('time', time)
formData.append('image',{
uri:uri,
mime:'image/jpeg',
name:`image${moment()}`
})
alert(JSON.stringify(formData));
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData,
};
fetch(`http://${Config.apihost}:${Config.port}/events`,config).then((res) => res.json())
.then((res) => {
this.setState({ modalVisible: false, name:'', date: moment().format('YYYY-MM-DD'), description:'', Location: 'AlHedaya Masjid' })
this.props.addEvent(res.message);
// this.props.navigation.goBack();
}).catch((err) => alert(err));
I have another screen which contains different number of pictures like gallery i am uploading multiple picture to the gallery, the request is working fine with code below.
const data = new FormData();
data.append('name', 'avatar');
images.map((res, i) => {
data.append('fileData', {
uri: res.path,
type: res.mime,
name: `image${i}${moment()}`
});
})
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: data,
};
fetch(`http://${Config.apihost}:${Config.port}/events/${this.state.item.id}/photos`, config)
.then((checkStatusAndGetJSONResponse) => checkStatusAndGetJSONResponse.json())
.then((response) => {
if (response.status && response.message.length > 0) {
var images = this.state.images;
response.message.map(file => {
images.push(`http:${Config.apihost}:${Config.port}/images/${file.id}`);
});
this.setState({ images });
}
}).catch((err) => { alert(err) });
I can't really see the difference between the two codes but the upper code giving me error.
- I am testing on android
- I am using the IP address instead of localhost (my others requests are working so thats out of equation)
- None of the solution in this link worked
React Native fetch() Network Request Failed
Am I missing something?
javascript reactjs react-native fetch
I have the following code for creating an event with a image and some body params. It was working fine when i was doing it without image, i am using react-native-image-crop-picker
for selecting images. I am getting "Network request failed" error when posting data from react-native. The request never reach my backend as i am getting no logs there. It is working fine with postmen.
MY CODE:
const { name, date, description, location, uri, mime, time } = this.state;
const formData = new FormData();
formData.append('name', name)
formData.append('date', date)
formData.append('description', description)
formData.append('location', location)
formData.append('time', time)
formData.append('image',{
uri:uri,
mime:'image/jpeg',
name:`image${moment()}`
})
alert(JSON.stringify(formData));
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData,
};
fetch(`http://${Config.apihost}:${Config.port}/events`,config).then((res) => res.json())
.then((res) => {
this.setState({ modalVisible: false, name:'', date: moment().format('YYYY-MM-DD'), description:'', Location: 'AlHedaya Masjid' })
this.props.addEvent(res.message);
// this.props.navigation.goBack();
}).catch((err) => alert(err));
I have another screen which contains different number of pictures like gallery i am uploading multiple picture to the gallery, the request is working fine with code below.
const data = new FormData();
data.append('name', 'avatar');
images.map((res, i) => {
data.append('fileData', {
uri: res.path,
type: res.mime,
name: `image${i}${moment()}`
});
})
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: data,
};
fetch(`http://${Config.apihost}:${Config.port}/events/${this.state.item.id}/photos`, config)
.then((checkStatusAndGetJSONResponse) => checkStatusAndGetJSONResponse.json())
.then((response) => {
if (response.status && response.message.length > 0) {
var images = this.state.images;
response.message.map(file => {
images.push(`http:${Config.apihost}:${Config.port}/images/${file.id}`);
});
this.setState({ images });
}
}).catch((err) => { alert(err) });
I can't really see the difference between the two codes but the upper code giving me error.
- I am testing on android
- I am using the IP address instead of localhost (my others requests are working so thats out of equation)
- None of the solution in this link worked
React Native fetch() Network Request Failed
Am I missing something?
javascript reactjs react-native fetch
javascript reactjs react-native fetch
edited Jan 2 at 21:40
Dacre Denny
13.9k41233
13.9k41233
asked Jan 2 at 21:36
Ahsan SohailAhsan Sohail
421312
421312
Shouldimages.push("http:${Config.apihost}:${Config.port}/images/${file.id}");
beimages.push("http://${Config.apihost}:${Config.port}/images/${file.id}");
?
– Dacre Denny
Jan 2 at 21:41
Thats when i actually post successfully to my server .. i am not even getting to my server the request is being stuck somewhere in the middle.
– Ahsan Sohail
Jan 2 at 21:46
Nice pick btw but the below code is working fine .. and now i am even more confused that why it is working.
– Ahsan Sohail
Jan 2 at 21:48
Yep - just a comment as I was looking at your code as it might be a problem later on
– Dacre Denny
Jan 2 at 21:48
Are you using Android emulator or real device for testing ?
– Tarreq
Jan 2 at 22:19
|
show 1 more comment
Shouldimages.push("http:${Config.apihost}:${Config.port}/images/${file.id}");
beimages.push("http://${Config.apihost}:${Config.port}/images/${file.id}");
?
– Dacre Denny
Jan 2 at 21:41
Thats when i actually post successfully to my server .. i am not even getting to my server the request is being stuck somewhere in the middle.
– Ahsan Sohail
Jan 2 at 21:46
Nice pick btw but the below code is working fine .. and now i am even more confused that why it is working.
– Ahsan Sohail
Jan 2 at 21:48
Yep - just a comment as I was looking at your code as it might be a problem later on
– Dacre Denny
Jan 2 at 21:48
Are you using Android emulator or real device for testing ?
– Tarreq
Jan 2 at 22:19
Should
images.push("http:${Config.apihost}:${Config.port}/images/${file.id}");
be images.push("http://${Config.apihost}:${Config.port}/images/${file.id}");
?– Dacre Denny
Jan 2 at 21:41
Should
images.push("http:${Config.apihost}:${Config.port}/images/${file.id}");
be images.push("http://${Config.apihost}:${Config.port}/images/${file.id}");
?– Dacre Denny
Jan 2 at 21:41
Thats when i actually post successfully to my server .. i am not even getting to my server the request is being stuck somewhere in the middle.
– Ahsan Sohail
Jan 2 at 21:46
Thats when i actually post successfully to my server .. i am not even getting to my server the request is being stuck somewhere in the middle.
– Ahsan Sohail
Jan 2 at 21:46
Nice pick btw but the below code is working fine .. and now i am even more confused that why it is working.
– Ahsan Sohail
Jan 2 at 21:48
Nice pick btw but the below code is working fine .. and now i am even more confused that why it is working.
– Ahsan Sohail
Jan 2 at 21:48
Yep - just a comment as I was looking at your code as it might be a problem later on
– Dacre Denny
Jan 2 at 21:48
Yep - just a comment as I was looking at your code as it might be a problem later on
– Dacre Denny
Jan 2 at 21:48
Are you using Android emulator or real device for testing ?
– Tarreq
Jan 2 at 22:19
Are you using Android emulator or real device for testing ?
– Tarreq
Jan 2 at 22:19
|
show 1 more comment
1 Answer
1
active
oldest
votes
In first code snippet you have written mime instead of type.
formData.append('image',{
uri:uri,
**mime:'image/jpeg**',
name:`image${moment()}`
})
it should be like below snippet
formData.append('image',{
uri:uri,
type:'image/jpeg',
name:`image${moment()}`
})
how did i miss that !!
– Ahsan Sohail
Jan 3 at 13:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013474%2freact-native-fetch-give-error-network-request-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In first code snippet you have written mime instead of type.
formData.append('image',{
uri:uri,
**mime:'image/jpeg**',
name:`image${moment()}`
})
it should be like below snippet
formData.append('image',{
uri:uri,
type:'image/jpeg',
name:`image${moment()}`
})
how did i miss that !!
– Ahsan Sohail
Jan 3 at 13:43
add a comment |
In first code snippet you have written mime instead of type.
formData.append('image',{
uri:uri,
**mime:'image/jpeg**',
name:`image${moment()}`
})
it should be like below snippet
formData.append('image',{
uri:uri,
type:'image/jpeg',
name:`image${moment()}`
})
how did i miss that !!
– Ahsan Sohail
Jan 3 at 13:43
add a comment |
In first code snippet you have written mime instead of type.
formData.append('image',{
uri:uri,
**mime:'image/jpeg**',
name:`image${moment()}`
})
it should be like below snippet
formData.append('image',{
uri:uri,
type:'image/jpeg',
name:`image${moment()}`
})
In first code snippet you have written mime instead of type.
formData.append('image',{
uri:uri,
**mime:'image/jpeg**',
name:`image${moment()}`
})
it should be like below snippet
formData.append('image',{
uri:uri,
type:'image/jpeg',
name:`image${moment()}`
})
answered Jan 3 at 6:52
ZubairHasanZubairHasan
1095
1095
how did i miss that !!
– Ahsan Sohail
Jan 3 at 13:43
add a comment |
how did i miss that !!
– Ahsan Sohail
Jan 3 at 13:43
how did i miss that !!
– Ahsan Sohail
Jan 3 at 13:43
how did i miss that !!
– Ahsan Sohail
Jan 3 at 13:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54013474%2freact-native-fetch-give-error-network-request-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Should
images.push("http:${Config.apihost}:${Config.port}/images/${file.id}");
beimages.push("http://${Config.apihost}:${Config.port}/images/${file.id}");
?– Dacre Denny
Jan 2 at 21:41
Thats when i actually post successfully to my server .. i am not even getting to my server the request is being stuck somewhere in the middle.
– Ahsan Sohail
Jan 2 at 21:46
Nice pick btw but the below code is working fine .. and now i am even more confused that why it is working.
– Ahsan Sohail
Jan 2 at 21:48
Yep - just a comment as I was looking at your code as it might be a problem later on
– Dacre Denny
Jan 2 at 21:48
Are you using Android emulator or real device for testing ?
– Tarreq
Jan 2 at 22:19