React Native getting response 400 when post to oauth/token using laravel passport - laravel-5

I am trying to login through oauth/tokens from my React Native project. I have tested with POSTMAN When i send the data from axios it works well. But when i try to send from my app it gives me Error: Request failed with status code 400
my code is :
axios({
method: 'post',
url: 'http://183.172.100.84:8000/oauth/token',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
data: {
client_id : '2',
client_secret : 'secret',
grant_type : 'password',
email : this.state.userEmail,
password : this.state.userPassword
}
})
.then((response) => {
console.log(response.data);
})

I've solved the problem in this way:
axios({
method: 'post',
url: 'http://183.172.100.84:8000/oauth/token',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8'
},
data: 'client_id=2&client_secret=secret&grant_type=password&email=' + this.state.userEmail+'&password='+this.state.userPassword
})
.then((response) => {
console.log(response.data);
})
Seems Axios have some problem to process the data in json format.
If you use Webpack you must also configure the proxy.

Related

Send xml file as payload in cypress post request

How to send xml file as payload in cypress post request:
When i am trying to send xml file/txt file as post request payload, am not able to do ad getting bad request
This works for me... (pardon the formatting)
it('Post File to URL', () => {
const yourFile = 'C:\\PathToYourFile\\YourFile.xml'
cy
.readFile(yourFile)
.then(function (text) {
cy.log(text)
postXML(text)
}
)})
function postXML(text) {
return cy.request({
url: 'https://yourURL.com',
method: 'POST',
body: text,
headers: {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'content-length': '1000',
'connection': 'close',
'soapAction': ''
}
})}

basic structure for converting ajax to axios?

Can some one give me example of how to convert ajax to axios ?
I am trying to convert this code into axios
$.ajax({
type: 'POST',
url: 'http://example.com/storeauthcode',
// Always include an `X-Requested-With` header in every AJAX request,
// to protect against CSRF attacks.
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
// Handle or verify the server response.
},
processData: false,
data: authResult['code']
});
axios.post('http://example.com/storeauthcode', authResult['code'], {
headers: {
'X-Requested-With': 'XMLHttpRequest'
'Content-Type: 'application/octet-stream; charset=utf-8',
},
transformResponse: (data) => { // do something with your data },
});
However a better place for the content-type would be the axios instance config itself.
Configure once globally:
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
Then per request:
const result = await axios.post(
'http://example.com/storeauthcode',
authResult['code'],
{
headers: {
'Content-type': 'application/octet-stream; charset=utf-8'
}
}
);

Issue in AJAX request in reactjs

I am trying to develop an application using reactjs as front end framework and laravel 5.6 as back end framework. I am trying to send AJAX request like below
import Auth from '../services/Auth';
var address_data = {
name :'foysal',
address :'foysal',
telephone_no:'foysal',
email :'foysal',
}
fetch('http://127.0.0.1:8000/api/addresses/store/',address_data, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + Auth.getToken(),
},
body: JSON.stringify()
})
.then((response) => response.json())
.then((responseData) => {
console.log(responseData);
})
I am getting below errors
Try changing your fetch request to initiate POST action verb like this
fetch('http://127.0.0.1:8000/api/addresses/store/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + Auth.getToken(),
},
body: JSON.stringify(address_data)
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));

Fetch API error on Mozilla Firefox

I am using javascript's fetch API to send a post request. In firefox, it will return an error below. Chrome is okay.
TypeError: arguments[0] is not a function
Code:
url = '/monitor_tasks/create';
params = {
method: 'post',
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-CSRF-Token': 'xxxxxxxxxxx'
},
data: JSON.stringify({patient: '1234'})
};
fetch(url, params);
Anybody had the same error?
RESOLVED:
- A 3rd party library caused this issue.

Passing from ajax to fetch on client side with a glassfish server

I have a phonegap application that uses
$.ajax(
type: 'POST,
dataType:"json",
url: 'test.com'
data: { mail: 'bob#test.com' }
)
which i get in my glassfish server doing something like
HttpServletRequest request;
request.getParameter('mail');
I'm moving my application in react native so i tryed
fetch('test.com', {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: 'mail=bob#test.com',
})
and i tryed
fetch('test.com', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ mail: 'bob#test.com' }),
})
but neither of these worked, i'm deseperately having request.getParameter('mail') == null on server side.How can i do ?Note : i don't want to change the way the server handles things.

Resources