I need to send the x-csrf-token along with the URL in a GET request. I am using request-promise nodejs package for this purpose, but I don't know how to do.
I need to do something like this:
return rp({
method: 'GET',
url: "https://alabbo.to/joiner?fid=5ba900635da0a&page=check",
CSRF: "Y5KLHznEcspsqDHgmy63UHvKZT8s48EuQ1bfv34n"
})
.then(function (html) {
}
CSRF is sent inside headers with key name X-CSRF-Token as shown below
return rp({
method: 'GET',
url: "https://alabbo.to/joiner?fid=5ba900635da0a&page=check",
headers: {
'X-CSRF-Token': "Y5KLHznEcspsqDHgmy63UHvKZT8s48EuQ1bfv34n"
}
}).then(function (html) {
})
Atishay is right, X-CSRF-Token is a header.
Otherway, is you use Node v8, you can use async / await instead of .then.
const response = await rp({
method: 'GET',
url: "https://alabbo.to/joiner?fid=5ba900635da0a&page=check",
headers: {
'X-CSRF-Token': "Y5KLHznEcspsqDHgmy63UHvKZT8s48EuQ1bfv34n"
}
})
Related
I have 2 files:
Test Specs
API layer (in which all API's are written like cy.request(). I am returning API response from here
I need to use the response of API in my test spec. But when I try to use the response I am getting undefined
That is how I returning my response
testFun() {
cy.request({
method: "GET",
url: `${this.objectFactory.url}/api-v7/listings/`,
headers: {
accept: "application/json",
Authorization: this.objectFactory.authorization,
cookie: this.objectFactory.cookie,
},
}).then((response) => {
return response;
}
});
});
}
And in spec file, I am calling this function and getting undefined
Can you try adding a return before the cy.request command?
testFun() {
return cy.request({
method: "GET",
url: `${this.objectFactory.url}/api-v7/listings/`,
headers: {
accept: "application/json",
Authorization: this.objectFactory.authorization,
cookie: this.objectFactory.cookie,
},
});
}
# then do
testFun().then((response) => { do something with response });
Return the request from the function, you don't need the .then() added.
testFun() {
return cy.request({
method: "GET",
url: `${this.objectFactory.url}/api-v7/listings/`,
headers: {
accept: "application/json",
Authorization: this.objectFactory.authorization,
cookie: this.objectFactory.cookie,
},
})
}
Use it in the test with a .then()
testFun().then(response => {
// assert response here
})
In my Cypress test, I am trying to make calls to two seperate API endpoints.
I'm able to make the calls, but I need to ensure they execute in the correct order.
Below are simplified versions of my requests:
cy.request('POST', apiUrl + 'Session', {username: merchant_username}
).as('postSession')
cy.request('POST', apiUrl + 'TestCase', {username: merchant_username}
).as('postTestCase')
It's important that the calls execute in this order because some of them depend on values from the others.
I am trying to retrieve sessionId from the postSession response:
cy.request({
method: 'POST',
url: apiUrl + 'session',
}).as('postSession')
cy.get('#postSession').should(response => {
sessionId = response.body.SessionId;
})
And then use it in the request body of postTestCase:
cy.request({
method: 'POST',
url: apiUrl + 'TestCase',
body: {
"SessionId": sessionId
}
})
If I do .then() after postSession & place postTestCase inside that like so, the request works fine, but I would like to avoid doing that if possible.
cy.get('#postToken').should(response => {
sessionId = response.body.SessionId;
}).then(() => {
cy.request({
method: 'POST',
url: apiUrl + 'TestCase',
body: {
"SessionId": sessionId
}
})
})
I've also tried using cy.wait(), but the sessionId is blank in the 2nd request then.
cy.wait('#postSession')
cy.wait('#postTestCase')
Is there a way I can ensure postSession is executed before postTestCase without placing postTestCase inside a .then() after postSession?
Unfortunately, at the time this answer is posted, there is an open issue on the Cypress GitHub repository with a proposal for await, as per this link.
So only the "nested" requests way is possible at the moment.
For example of your snippets:
cy.request({
method: 'POST',
url: apiUrl + 'session',
}).then((response) => {
const sessionId = response.body.SessionId;
cy.request({
method: 'POST',
url: apiUrl + 'TestCase',
body: {
"SessionId": sessionId
},
});
});
You need to do something like this:
cy.request({
method: 'GET',
url: 'https://' + host + '/lending/loan',
headers: default_headers
}).then(res => {
cy.request({})
.then(res => {})
})
I am using Cypress 4.3.0 version, the baseUrl = "https://tenant-demo.somesitedev.net" has been set in cypress.json file. While I am sending the cy.request() command, it is sending multiple request (please see Fig:1) . Also, when I observed the visit command I could see following Original Url, Resolved Url and Redirects. In this scenario, how do I login to the site using cy.request() command.
before(()=>{
cy.visit('/').then(()=>{
cy.get('input[type="hidden"]').invoke('val').then((val)=>{
const token = val;
cy.login(token);
})
})
})
Cypress.Commands.add('login', (token) => {
const username= 'test1.user';
const password= 'somepassword';
const accessToken = localStorage.getItem('tokens');
const cookieValue = document.cookie.split(';');
const configCat = localStorage.getItem('ConfigCat_');
cy.request({
method: 'GET',
url: '/dashboard',
failOnStatusCode: false,
form: true,
body:{
_token: token,
username,
password
},
headers: {
'accept': 'text/html',
'content-type': 'application/x-www-form-urlencoded',
'authorization': `bearer ${accessToken}`,
'ConfigCat_': `${configCat}`,
'cookie': `${cookieValue}`
}
}).then((res)=>{
visitDashboard();
})
})
const visitDashboard = () => {
cy.visit('dashboard')
}
Fig:1
Fig:2
Somehow I managed to find a way to resolve the problem. Since the baseUrl has got some path extension /auth/login, whenever I trigger a cy.request() it was always redirecting back to login page even though the credentials was correct. Also there were two requests in the console.
So the way I did was to send another cy.request() with GET method with body params immediately after the first POST cy.request() with qs parameters. From the request headers I find out there was a 'token' submitted every time when the user login.
If there is another easy way let me know.
Cypress version : 4.4.0
Inside beforeEach(), grab the 'token' value;
beforeEach(() => {
cy.visit('/');
cy.loadTokens();
cy.get('input[name="_token"]').invoke('val').then((val)=>{
const tokenValue = val;
cy.loginRequest(tokenValue);
})
})
Following is the commands.js file:
Cypress.Commands.add('loginRequest', function (tokenValue) {
return cy.request({
method: 'POST',
url: Cypress.config('baseUrl'),
followRedirect: true,
headers: {
'content-type': 'text/html; charset=UTF-8'
},
qs:{
_token: tokenValue,
username: 'your_username',
password:'your_password'
}
}).then(()=>{
return cy.request({
method: 'GET',
url: 'https://tenant-demo.somesitedev.net/dashboard',
followRedirect: false,
headers: {
'content-type': 'text/html; charset=UTF-8'
},
body:{
_token: tokenValue,
username: 'your_username',
password:'your_password'
}
})
})
});
i gonna send a get request, but i need to send with a body,
when i use a postman it works well, but if i use axios, it dosent't works.
i use axios in javascript
and i use postman
var settings = {
"url": "http://127.0.0.1:5000/user/history",
"method": "GET",
"processData": false,
"data": "{"summoner":"몽디로 맞아봐"}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
axios({
url: 'http://127.0.0.1:5000/user/history',
method: 'get',
data: {
'summoner': '몽디로 맞아봐'
}
});
i expect this code works
ES6:
import axios from 'axios';
let requestHeader, data;
data = {
'summoner': '몽디로 맞아봐'
}
requestHeader = {
'Content-Type':'application/json'
}
axios.get('http://127.0.0.1:5000/user/history', {
params: data,
headers: requestHeader
}).then((response) => {
console.log(response)
})
In HTTP specification, sending GET request with body is ok, you can try it with http module in nodejs.
But, axios' implementation whichs bases on XHR will remove all the body of GET request before sending.
Fetch api do the same thing like axios.
I am using AngularJS to send form data to the server.
app.controller("OrganizerReg", ["$scope","$http", function($scope,$http) {
$scope.userOrganizer = {};
$scope.processform = function() {
$http ({
method: 'POST',
url: 'http://52.11.67.77:3000/api/users',
headers: {'Content-Type': 'application/json'},
data: JSON.stringify($scope.userOrganizer)
})
.success(function(response) {
console.log(response.user_id);
$scope.user_id = response.user_id;
})
$http ({
method: 'POST',
url : 'http://52.11.67.77:3000/api/users/'+$scope.user_id+'/accessTokens',
})
.success(function(response) {
console.log(response);
})
}
In order to get access Token , I need to send a POST request.The response from first HTTP request , gives the user_id which is needed for generating the access token. The problem is whenever I send the request, the $scope.user_id returns undefined in the header. How to set my $scope.user_id global, so that it can be used by another HTTP request.
EDIT
I read the duplicate question but it still hasn't helped me.
use promise chains to call the request after another
function firstReq(){
return $http({
method: 'POST',
url: 'http://52.11.67.77:3000/api/users',
headers: {'Content-Type': 'application/json'},
data: JSON.stringify($scope.userOrganizer)
})
}
function secondReq(){
return $http({
method: 'POST',
url : 'http://52.11.67.77:3000/api/users/' +$scope.user_id + '/accessTokens',
})
}
$scope.processform = function() {
firstReq()
.then( function( response )
{
console.log(response.data.user_id);
$scope.user_id = response.data.user_id;
return secondReq();
})
.then(function(response){
console.log(response.data);
})
}