file:/// url doesn't work in FormData [React Native] - ios

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

Related

"Only absolute URLs are supported" When fetch webhooks Nextjs

Im trying to fetch an webhooks url using Environment Variables.
here is my code
const url = process.env.WEBHOOK_URL;
const response = await fetch(
`${url}` ,
{
body: JSON.stringify({
name,
email,
message,
}),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
}
);
But the error show up "Only absolute URLs are supported"
please help! Thank you

Zapier API Configuration: send form-data instead of json request body

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'.

uploading user image to strapi using axios

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"

(angular 8) sending file data in $http Post data gives {} on rails API logs

angular 8, Rails 5.2 API
I am trying to attach an image to my Event model in edit form. I am following this tutorial
my typescript code:
const httpOptions = {
headers: new HttpHeaders({
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8; multipart/form-data',
Authorization: environment.apiAuthKey,
})
};
updateEvent(event: Event): Observable<Event> {
return this.http.put<Event>(url, event, httpOptions)
.pipe(
...
}
This generates these logs on Rails API side:
Started PUT ...
Processing by ... as JSON
Parameters: {"title"=>"testing image", "image" => {}}
...
Any idea why image param is being null?
So far, from my research I got an idea that I have to put multipart-formdata in my HTTP headers, I did that but no change in output.
Try to use formData instead of FormGroup
const formData = new FormData();
formData.append('image', image);
formData.append('title', testing image);
this.http.post(url, formData);
you can also do a test to post without setting HttpHeaders, FormData will set the suitable header for you

POST to VoilaNorbert API from Rails with HTTParty

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
}
)

Resources