Axios request with Contact forms 7 - axios

I'm using contact forms 7, and in my form submission I use this function:
const formData = {
'your-name':'John Doe',
'your-email': 'johndoe#mail.com',
}
axios({
method: 'POST',
url: 'https://0xsociety.com/wp-json/contact-form-7/v1/contact-forms/258/feedback',
headers: {
"content-type": "multipart/form-data",
},
data: formData,
})
But it doesnt work, i get errors saying fields are empty.
But I tried with postman and it works perfectly when i do it manually
How would I get my axios post request to mimic this:
https://gyazo.com/e1ffe5bcc3f943a074d9b9e2c32b162d

I figured this out by using postman in my app:
import request from 'postman-request'
const formData = {
'your-name': name,
'your-email': email,
'your-subject': inquiries.find(x=> x.value === inquiry).text,
'file-871': file
}
request.post('https://your-domain/wp-json/contact-form-7/v1/contact-forms/your-form-id/feedback',{ form: formData}, function(err,httpResponse,body){
if(err) {
console.log(err)
}
else {
console.log(body)
}
})

Related

error 403 url shortener with bitly and js

I have searched for the answer but nothing helped. Please suggest me with a solution, when I try to shorten the url with bitly and vue js I get 403 error.
axios api:
const headers = {
'Authorization': `Bearer ${myToken}`,
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
'Access-Control-Allow-Credentials':true
};
const dataString =
'{ "long_url": "https://dev.bitly.com", "domain": "bit.ly", "group_guid": "Ba1bc23dE4F" }';
axios
.post("https://api-ssl.bitly.com/v4/shorten", {
headers: headers,
body: dataString,
})
.then(function (response) {
if (response.status == 200) {
console.log(response);
} else {
console.log("Opps dude, status code != 200 :( ");
}
})
.catch(function (error) {
console.log("Error! " + error);
});
I have changed code according proposal in the comments. But the same 403 error.
I have just received bitly support email that it is not working because I use group_id. "... "Ba1bc23dE4F" is a placeholder value in our documentation to show where your group GUID would be passed if you have an account with multiple groups. In your case, you can remove this whole value and simply pass a body with:
{ "long_url": "https://dev.bitly.com", "domain": "bit.ly" }"

Axios + Ionic React + Sails: _csrf token 403 Forbidden

I am developing an API with Sails.js and an user App with Ionic-React. At page load I make an axios request to get the _csrf token. When I submit the data from a login form to sails I always get a 403 Forbidden response. I disabled csrf (config/security.js) in sails and then I could retrieve the response. I am sending the token in the header.
I am trying too to get the session cookie but its not working I think that might be why the server refuses the request.
Ionic App:
componentDidMount(this: this) {
axios.get('http://localhost:1337/api/v1/security/grant-csrf-token')
.then(response => {
const _csrf = response.data._csrf
this.setState({
form: {
...this.state.form,
_csrf: _csrf,
}})
});
}
OnSubmit:
const { emailAddress, password, _csrf } = this.state.form;
const config= {
data: {
"emailAddress": emailAddress,
"password": password,
},
headers: {
"x-csrf-token": _csrf
},
withCredentials: true,
jar:cookieJar,
};
axios.post('http://localhost:1337/api/v1/users/authenticate', null, config)
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
})};
On Chrome DevTools Network Response:
On Postman this same request works and I get a 200 with the user data, and the request does include the sails.sid cookie.
I do not want to disable csrf protection, that wouldn't be a solution. Is it the sails.sid cookie that I am missing?
I am using this,
axios({
method: 'post',
crossdomain: true,
url: apiFormUrl,
data: formData,
headers: {
"Content-Type": "multipart/form-data",
"Authorization": access_token
}
})
.then
and it works

send axios request multipart/files

I am trying to upload a photo to a webservice and the documentation clearly specifies that the request type is Multipart/files so I am trying this request with axios which always fails.
HTTP.put(`/employees/${response.data.data.id}/photo`, { photo: this.image }, {headers: { put: { 'Content-Type': 'multipart/files' }}})
.then((profilePictureResponse) => {
console.log(profilePictureResponse);
})
.catch((error) => {
console.log(error.response);
});
HTTP is an axios instance with default configurations but the default content-type is set to application/json

How to create users using external API in owncloud

My application creates owncloud users using external APIs.
I tried with get owncloud version using external API. Below is the code I used:
$.ajax({
type: 'GET',
url: 'http://localhost/owncloud/index.php/apps/news/api/2.0/version',
contentType: 'application/json',
success: function (response) {
// handle success
},
error: function () {
// handle errors
},
beforeSend: function (xhr) {
var username = 'admin';
var password = 'admin';
var auth = btoa(username + ':' + password);
xhr.setRequestHeader('Authorization', 'Basic ' + auth);
}
});
The above code didn't work.
How to achieve this?
Thanks in advance.
The code you have put is right.
You could as well test if your api is returning the data using something like postman or chrome rest client.
then(function(response) {
$.ajax({
url: url,
type: "POST",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(data),
})
.done(function(res) {
swal("Deleted!", "Your ListJds has been deleted.", "success");
})
.error(function(res) {
res;
swal("Delete Failure!", "Please Try Again.", "error");
});
})

ember-simple-auth facebook authenticator

Trying to hook up facebook authentication for my app. I've got the backend working correctly but my authenticator (I think this is the problem) is always returning undefined.
Copying the example from from ember-simple-auth the console log on line 23 is never called, making me think something else the issue?
import Ember from 'ember';
import Torii from 'ember-simple-auth/authenticators/torii';
const {inject: {service}} = Ember;
export default Torii.extend({
torii: service(),
ajax: service(),
authenticate() {
const ajax = this.get('ajax');
return this._super(...arguments).then((data) => {
return ajax.request('http://localhost:8080/api/login', {
type: 'POST',
dataType: 'application/json',
data: {
'grant_type': 'facebook_auth_code',
'auth_code': data.authorizationCode
}
}).then((response) => {
console.log("CALLED!");
return {
access_token: response,
provider: data.provider
};
});
});
}
});
The response from the server is the access_token from facebook;
How can I better debug/solve what's going on here?
The problem was actually a simple error with the dataType used. It should be dataType: 'json' not dataType: 'application/json'