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': ''
}
})}
Related
So based off Cypress request docs: https://docs.cypress.io/api/commands/request.html
It seems like I should be able to send a POST request with a JSON body pretty easily. So this is what I tried:
cy.fixture('test_create').as('create_test')
cy.request({
method: 'POST',
url: 'http://localhost:8080/widgets',
body: '#create_test',
headers: {
'Authorization': this.token,
'Content-Type': 'application/json;charset=UTF-8'
}
})
However when I look at the "commands" Cypress sends, it's sending the body as literally Body: #create_test
Is it not possible to use a fixture within the body of a POST request? I confirmed the fixture is loading correctly. I confirmed also it works when I paste the entire JSON inside of the body option....but that gets ugly really quick with large JSON bodys.
You get a literal because in the form cy.request(options), options is a plain JS object and unfortunately not parsed by Cypress to interpret the alias.
The request form cy.request(method, url, body) probably does allow an alias for the body param, since cy.route() allows it ref: Accessing Fixture Data
e.g the following should be valid, but does not allow setting headers
cy.fixture('test_create').as('create_test')
cy.request('POST', 'http://localhost:8080/widgets', '#create_test');
So, you can use then()
cy.fixture('test_create').then(myFixture => {
cy.request({
method: 'POST',
url: 'http://localhost:8080/widgets',
body: myFixture,
headers: {
'Authorization': this.token,
'Content-Type': 'application/json;charset=UTF-8'
}
})
});
or
cy.fixture('test_create').as('create_test');
... // some other code between
cy.get('#create_test').then(myFixture => { // retrieve the fixture from alias
cy.request({
method: 'POST',
url: 'http://localhost:8080/widgets',
body: myFixture,
headers: {
'Authorization': this.token,
'Content-Type': 'application/json;charset=UTF-8'
}
})
})
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'
}
}
);
When I'm running cypress e2e tests, application makes XHR requests. How can I log all this requests and responses? I don't want to stub these requests. I with to get an artifact with all requests and responses made during test. Gitlab is used as CI.
Main test code looks like this. All these are user defined commands, interacting with the application. Interacting with the application causes different requests to be made (e.g. I click a button, this causes the request).
it('Log response to a file',function(){
cy.request({
method: 'GET',
url: 'https://<site>/home/payments/currency/confirm/*',
headers: {
'Content-Type': 'application/json',
},
body: {},
}).then((response)=>{
const someResponse = response.body;
console.log("hhhh"+someResponse);
cy.writeFile('cypress/fixtures/testResponse.json', someResponse);
cy.login(login_name, pass)
cy.typeOTPpinpad(secret)
cy.makePayment('Currency', 'amount')
cy.typeToken(secret)
cy.logout()
})
})
Here is how I tried to use regular expression to catch request (id is unique and I need to use regular expressions).
https://<mysite>/home/payments/<currency>/confirm/* - asterisk is payment id.
You could grab the request and response and write to a location as below. I have write the request and response to fixture folder as below: Try the below and let me know
it('Log request to a file',function(){
cy.request({
method: 'GET',
url: 'url_here',
headers: {
'Content-Type': 'application/json',
},
body: {},
}).then((request)=>{
const someRequest = JSON.stringify(request);
console.log("hhhh"+someRequest);
cy.writeFile('cypress/fixtures/testRequest.json', someRequest);
})
})
// The below is for response:
it('Log response to a file',function(){
cy.request({
method: 'GET',
url: 'url_here',
headers: {
'Content-Type': 'application/json',
},
body: {},
}).then((response)=>{
const someResponse = response.body;
console.log("hhhh"+someResponse);
cy.writeFile('cypress/fixtures/testResponse.json', someResponse);
})
})
The testrunner has such information on board:
[
For one of my web service testing, I need to read an xml file and assign the contents of the same to cy.request body.
How can I achieve this?
I tried the below method and was not able to successfully pass the XML to the body.
Please let me know.
eg:
cy.readFile('Desktop/Testing/W1.xml')
.then(text1 => {
console.log(text1);
cy
.request({
url: 'my URL',
method: 'POST',
body: {text1},
headers: {
'Authorization':'Basic ........',
'content-type': 'application/......-v1.0+xml',
'Accept':'application/...v1.0+json,application/....-v1.0+json'
}
})
.then((response) => {
assert.equal(response.status, 200, "status was 200");
cy.log("Response Body",response.body);
console.log("Response Body",response.body);
})
})
I suggest something like this:
Prepare function for fetching XML
function fetchXML(text) {
return cy.request({
url: 'my URL',
method: 'POST',
body: text,
headers: { ... }
})
}
Then call readFile and pass to promise callback result
cy
.readFile('Desktop/Testing/W1.xml')
.then(text => fetchXML(text)) // or just .then(fetchXML)
.then(responseFromXML => { ... })
and i second callback you can use response from XML fetch
Link to docs about Cypress.Promise LINK
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.