How to understand GM_xmlhttpRequest parameter "binary" - gm-xmlhttprequest

I tested below code:
`GM_xmlhttpRequest({
method: 'POST',
url: "some url",
data: "a1b2",
binary: false});
GM_xmlhttpRequest({
method: 'POST',
url: "some url",
data: "a1b2",
binary: true});`
I used Fiddler to check what's really send. but seems the two code sent same raw data by http.
I also tested the code below
GM_xmlhttpRequest({
method: 'POST',
url: "some url",
data: new Blob([1,2,3]),
binary: false});
GM_xmlhttpRequest({
method: 'POST',
url: "some url",
data: new Blob([1,2,3]),
binary: true});
but the two code send identical raw data

Related

Cypress send image in request

I am trying to send image as a part of POST request, any idea how i can do this?
Request:
cy.request({
method: 'post',
url: '/some/url',
body: {
'name': data.Name,
'slug': data.Slug,
'new_preview_image': 'images/test.jpeg',
},
headers: {
accept: 'application/json'
},
auth: {
'bearer': ''
}
});
That is the request i am trying to send, but for image i am getting response
Body: {
"message": "The new preview image must be an image."}

How to handle the plain/text POST request in the cypress

I have a postman collection and It's POST call and the request body is type of plain/text and I just want to automate this using cy.request but I'm not sure how to pass the test body in the cy.request body section and it returned 400 bad request if I run the below code.
cy.request({
url: `${url}/user`,
method: "POST",
headers: {
'Content-Type': 'plain/text'
},
body: {
"confirmEmail": "true"
}
}).then(res =>{
cy.task('log',"Email id "+res.body.emailAddress);
return res.body;
});
}
The above request return .json response but the input request if text format and the same working fine in the postman tool.
Passing the request body in the below format in the postman tool and its working fine.
confirmEmail=true
My assumption is in the request body our endpoint is expecting a boolean value, but you are passing a string. So changing "confirmEmail": "true" to "confirmEmail": true should work.
cy.request({
url: `${url}/user`,
method: 'POST',
headers: {
'Content-Type': 'plain/text',
},
body: {
confirmEmail: true,
},
}).then((res) => {
cy.log(res.body.emailAddress) //prints email address from response body
})
In case you need to pass parameters in your URL you can directly use qs
cy.request({
url: `${url}/user`,
method: 'POST',
qs: {
confirmEmail: true,
},
headers: {
'Content-Type': 'plain/text',
},
}).then((res) => {
cy.log(res.body.emailAddress) //prints email address from response body
})

How to upload video directly from client browser to Viemeo using jquery?

I have try to upload a video to vimeo account directly from browser using api ,video details are created but file seems to corrupted/upload not happens.This is my sample code.
var file = $(this).prop("files")[0];
var formData = new FormData();
formData.append("file_data", file);
$.ajax({
url: "https://api.vimeo.com/me/videos/",
type: "post",
data: formData,
headers: {
"Authorization": "Bearer -----",
},
processData: false,
mimeType: "multipart/form-data",
contentType: false,
}).done(function (response) {
// Do something
}).complete(function (response) {
// Do something
}).fail(function (e) {
// Do something
});
vimeo video listing shows blank thumbnail
enter image description here
Try this piece of code. I have made some changes here:
var file = $(this).prop("files")[0];
var formData = new FormData();
formData.append("file_data", file);
$.ajax("https://api.vimeo.com/me/videos/", {
type: "POST",
headers: {
"Authorization": "Bearer -----",
},
data: formData,
contentType: "multipart/form-data", // changed this
dataType: "json",
crossDomain: true // for CORS policy error
}).done((response) => {
// Do something
}).fail((error) => {
// Do something
}).complete(() => {
// Do something
});
I have chaged contentType and removed mimeType. I've also removed un-necessary processData field.

New axios request in response

I'm posting to an API using axios, and using the data from the response I want to make another API request but I'm running into issues saying that axios is not defined.
The calls, that are in inside my Vue login component, are the following:
this.axios({
method: 'post',
url: '/my_api/test',
data: "testdata=test123",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (response) {
this.axios({
method: 'get',
url: '/my_api/test2',
data: "testdata2=" + response.data.id
})
})
and as I mentioned earlier, the error in my console is the following: TypeError: Cannot read property 'axios' of undefined.
I´ve tried writing the second request without this. but I'm having the same issues. Where am I going wrong?
You have to use an arrow function here.
Regular functions have their own this value.
this.axios({
method: 'post',
url: '/my_api/test',
data: "testdata=test123",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => {
this.axios({
method: 'get',
url: '/my_api/test2',
data: "testdata2=" + response.data.id
})
})
Or if you prefer old-fashioned way you can save this reference to some variable to have access to it in the callback:
const self = this
this.axios({
method: 'post',
url: '/my_api/test',
data: "testdata=test123",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (response) {
self.axios({
method: 'get',
url: '/my_api/test2',
data: "testdata2=" + response.data.id
})
})

Amazon Gateway API with Lambda - Could not parse request body into json

I seem to be getting a issue with Amazon Gateway API not liking my sent params for example.
$.ajax({
url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
type: "POST",
data: {
"device": "test",
"datetime": "1446757400919"
},
success: function (returnhtml) {
console.log(returnhtml);
$("#result").append("DOES NOT WORK - <br>" + JSON.stringify(returnhtml));
}
});
$.ajax({
url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
type: "POST",
data: {},
success: function (returnhtml) {
console.log(returnhtml);
$("#result").append("<br>WORKS ???? - <br>" + JSON.stringify(returnhtml));
}
});
Here is a working example.
http://jsfiddle.net/Uwcuz/4315/
Can someone let me know why it wont let me send params every time i add a parameter i get this error.
{
Type = User;
message = "Could not parse request body into json.";
}
OK THIS WORKS BUT SEEMS A BIT WEIRD TO ME.
$.ajax({
url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
type: "POST",
data: "{\"device\": \"test\",\"datetime\": \"1446757444524\"}",
success: function (returnhtml) {
console.log(returnhtml);
$("#result").append("WORKS - <br>" + JSON.stringify(returnhtml));
}
});
The problem lies in how you send the data to API Gateway.
Without knowing the details of your API configuration I am guessing that you have a Request Mapping setup for application/json.
jQuery will by default post your data as application/x-www-form-urlencoded but you want to send it as json.
You can do this without having to fiddle too much with the data yourself:
var requestParams = {
url: "https://tibqwxuqoh.execute-api.us-east-1.amazonaws.com/dev/getitems",
method: "POST,
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
"device": "test",
"datetime": "1446757400919"
});
};
var request = $.ajax(requestParams);
The key here is JSON.stringify() and telling jQuery that the dataType is json as well as setting the contentType to application/json.