Skip to content

Instantly share code, notes, and snippets.

@Obooman
Created May 16, 2016 05:27
Show Gist options
  • Save Obooman/da1137703c47055027c420480df76075 to your computer and use it in GitHub Desktop.
Save Obooman/da1137703c47055027c420480df76075 to your computer and use it in GitHub Desktop.
react-native bulit-in tool for image upload
export function postData (url, params, fileURL) {
let data = new FormData()
if (fileURL) {
data.append('image', {uri: fileURL, name: 'image.jpg', type: 'image/jpg'})
}
_.each(params, (value, key) => {
if (value instanceof Date) {
data.append(key, value.toISOString())
} else {
data.append(key, String(value))
}
})
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data; boundary=6ff46e0b6b5148d984f148b6542e5a5d',
'Content-Language': React.NativeModules.RNI18n.locale,
'Authorization': 'Token ABCDEF123457890',
},
body: data,
}
return fetch(API_URL + url, config)
.then(checkStatusAndGetJSONResponse)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment