I'm uploading an image to the users model in strapi using axios post request
Code
let bodyFormData = new FormData();
bodyFormData.append('files', this.state.avatars, this.state.avatars.name)
bodyFormData.append('ref', 'users')
bodyFormData.append('refId', '1')
bodyFormData.append('field', 'avatar')
bodyFormData.append('source', 'users-permissions')
console.log(bodyFormData)
axios({
method: 'post',
url: `${strapi}/upload`,
data: bodyFormData,
headers: {
'Content-Type': 'multipart/form-data',
}
}).then(res=>console.log(res)).catch(err=>{console.log(err.response.data.message)})
Uploading Image to Strapi
I'm targeting the avatar field
however, I'm getting an internal server error
how to fix this? need help
Try
axios({
method: 'post',
url: `${strapi}/upload`,
data: JSON.stringify(bodyFormData),
headers: {
'Content-Type': 'multipart/form-data',
}
})
Update the key "ref" to be "User" only not "Users"
Related
In my react-native app when I take a picture using react-native-vision-camera or resize an image via expo-image-manipulator, I got the following url for the final file/image:
file:///var/mobile/Containers/Data/Application/CB881575-5B7D-4B1D-A016-CF6A4F025AB4/Library/Caches/ImageManipulator/F1233844-82D9-4281-9865-7B382F3F0F84.jpg
Then I create a FormData to upload the source via axios:
formData.append(name, {
uri: uri.replace("file://", ""),
type: "image/png",
name: "filename.png",
});
Note that I replace file:// in the url, as many answers both here and on GitHub said.
Finally:
axios.post(url, formData, {
headers: {
Authorization: "Token ...",
"Content-Type": "multipart/form-data",
},
transformRequest: (formData) => formData,
});
And it doesn't work, the image isn't uploaded on the backed, if I try to print the form data too there's no images inside.
Note: both image/png or image/jpg don't work.
Does someone have had the same issue?
Thank you
I am setting up a Zap for our application in Zapier.
However, I've run into some trouble having the Zap pass over the data in the correct format.
By default it appears Zapier passes the data as json request body, but our backend only accepts form-data.
Is it possible to configure the Zap to send over form-data instead?
In the code below, I've tried to send the data as both params and body, but my backend doesn't any of it as form-data:
const options = {
url: '${URL}',
method: 'POST',
headers: {
'Authorization': ${token},
'Content-Type': 'application/json',
'Accept': 'application/json'
},
params: {
'phone': bundle.inputData.phone,
'email': bundle.inputData.email,
'dialog': bundle.inputData.dialog,
'name': bundle.inputData.name
},
body: {
'name': bundle.inputData.name,
'email': bundle.inputData.email,
'phone': bundle.inputData.phone,
'dialog': bundle.inputData.dialog
}
}
return z.request(options)
.then((response) => {
response.throwForStatus();
const results = z.JSON.parse(response.content);
// You can do any parsing you need for results here before returning them
return results;
});
Any input is greatly appreciated!
I fixed it by replacing 'Content-Type': 'application/json' with 'Content-Type': 'application/x-www-form-urlencoded'.
I am trying to POST data to the standard VoilaNorbert search API endpoint using the HTTParty Rails gem:
response = HTTParty.post(
'https://api.voilanorbert.com/2018-01-08/search/name',
query: {
'name': 'Elon Musk',
'domain': 'https://www.tesla.com/'
},
headers: {
'Content-Type': 'application/json',
'Authorization': VOILANORBERT_API_TOKEN
}
)
However, when I try this simple request, I get the response:
=> {"name"=>["This field is required."]}
My understanding is that the query block above is supposed to pass along the name field, which in this case is "Elon Musk".
What am I missing here?
After some trial and error, I succeeded by realizing I needed to change the payload to send form-data in the body of the request rather than as query params. This requires using the body block to encode form fields, as well as the multipart method on the request indicating that data is being uploaded.
response = HTTParty.post(
'https://api.voilanorbert.com/2018-01-08/search/name',
body: {
name: 'Elon',
domain: 'https://www.tesla.com/'
},
multipart: true,
headers: {
'Content-Type': 'application/json',
'Authorization': VOILANORBERT_API_TOKEN
}
)
I thought that Rails automatically identifies/parses JSON params if the request is recognized as JSON. But the request below:
Processing by Api::V1::LinksController#create as JSON
Parameters: {"link"=>"{\"title\":\"My first title\"}"}
And the following params method:
def link_params
params.require(:link).permit(:title)
end
Results in this error:
NoMethodError (undefined method `permit' for "{\"title\":\"My first title\"}":String):
Any ideas what the convention here is to get strong params + json working would be much appreciated.
Update
Here's the code that makes the request (with the http client axios):
axios({
method: 'post',
url: '/api/v1/links.json',
responseType: 'json',
params: {
link: {
title: "My first title"
}
},
})
.then( (response) => {
});
As per the docs here
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
Replace params: with data:.
In my current iOS project I need to fetch data from api call with POST method along with login credentials(userName & Password) as authentication header in react native javaScript file.
Can someone Please help me on that.
refernce screen shot
Just set the fetch method to 'POST', add in headers and body as key-value pairs and process response. Here is an example.
var bodyMap = {};
// fill in the body map with keyvalue pair
fetch(POST_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': authValue,
'Content-Type': 'application/json',
},
body: JSON.stringify(bodyMap)
}).then((response) => response.json())
.then((responseData) => {
console.log(responseData);
//process response
})
.catch((error) => {
console.warn(error);
});