How do I regex a Content-Disposition header value in postman? - testing

I am adding some tests into my postman API request and I was wondering how I can give a regex into the Content-Disposition header value, I have something like this but date is dynamic and just wanted to regex or do some other approach, any ideas?
pm.test("My test file name is downloaded successfully", function () {
pm.response.to.have.header("Content-Disposition", "attachment; filename=Test Project_information_10_15_2021.xlsx");

You can do this:
pm.test("My test file name is downloaded successfully", () => {
const content = pm.response.headers.get("Content-Disposition");
pm.expect(content).to.match(/^attachment.+xlsx$/);
});

Related

Difficulty to recover an audio file with axios from API

I have a problem when obtaining my "instrumental" object which contains a field which is supposed to be an audio file in byte array. The procedure is as follows: I send an HTTP request in post with axios to my Backend (Spring) and I obtain my object in JSON format in my Frontend.
Instrumental object
ReactJS (Frontend) Axios Request
axios
.post("/instrumentales/dernierajout", null, {
headers: {
Authorization: "Bearer " + getCookie("auth_token") + "",
},
})
.then((res) => {
if (res.data != null) {
setLstInstru(res.data);
}
});
Spring(Backend)
#PostMapping("dernierajout")
List<Instrumentales> DerniersAjout(){
List<Instrumentales> instru =instrumentalesRepository.getDerniersAjout();
return instrumentalesRepository.getDerniersAjout();
}
Then I'm stuck with my "untagged" field which is in base64 type application/octet-stream. I would like to have in my untagged field the byte array from my database as a blob with a mp3 or wav type in order to generate an object link like this : "blob:http://localhost:3000/ca78b34a-1252-4983-b601-7dd471a635a7".
I tried to see if it was possible to convert the application/octet-stream type base64 but it is obviously impossible, I also tried to change the type of response provided by axios but it does not change anything to the problem because I need the other JSON fields.
If someone had a similar problem and can give me an idea I'm interested .

Axios prepending content to start of file making it unreadable

I am trying to upload a file to an s3 presigned using axios from an expo managed mobile app FE. I have found that the following code works perfectly:
const file = await fetch(fileRef.uri);
const blob = await file.blob();
await fetch(uploadUrl, { method: 'PUT', body: blob });
here fileRef is an object like:
Object {
"height": 1920,
"uri": "file:///....jpg",
"width": 1080,
}
and uploadUrl is a presignedURL
I want to port this over to axios to take advantage of the onUploadProgress event. I've written the following:
const body = new FormData()
body.append('file', fileRef)
await axios.put(uploadUrl, body);
This uploads the file, however it prepends additional information to the start of the file that makes it so the image or video uploaded is not readable. The information it prepends looks like:
--9V.XUQuQ1DIG8HFMzJO-veI4JbmI7j_WawYPxtMUG2NhK_7eGnlL.kVNSXyH_sAQ2897mg^M
content-disposition: form-data; name="file"^M
content-type: image/jpeg^M
^M
I found that if i delete these lines, the file can now be opened (ex. by Quicktime).
I'd like to know how i can not have this information added to the start of the file?

Express Set Custom Parameter Query Starter

I'm using express to interact with discord's oauth2 api.
When I request a user oauth token the server responds with a url like:
http://localhost:3000/index#token_type=Bearer&access_token=tkn&expires_in=int
I'm trying to extract the parameters after the # as with discords api parameters start with # unlike others which start with a ?
Because it doesn't start with a question mark I am unable to use the req.params.x property.
I thought, "No big deal, ill just get the url and extract it myself" but every single url accessor in express removes string after #. This includes req.url and req.originalUrl which both return the file path.
So how can I get url parameters started by hashtags instead of question marks?
Or How can I get the full url with content after hashtags
I was able to solve this problem by setting a custom query parser. Code snippet below.
const app = express();
app.set('query parser', (query) => {
return query.split('&').map(item => {
const [key, value] = item.split('=');
return {
key,
value
}
});
});
app.get('/', (req, res) => {
console.log(req.originalUrl); // Full URL starting with file path
})

Node.js puppeteer - Transforming the fetch URL

Original URL :
https://goto-example.com/a/r/Acc/0017A000WCJrHQAX/view
I need to modify my url to
https://goto--example.com/a/r/Acc/0017A000WCJrHQAX/related/Opportunities/view
I need to replace ' /view' from the original url and replace it with 'related/Opportunities/view'.
Is there any other function in pupeeteer to modify the URL and continue my execution
please help
You can use setRequestIntercept and the request event. For instance, this piece of code will return the same image for every image request:
await page.setRequestInterception(true);
page.on('request', request => {
const url = request.url().endsWith('.jpg')
? 'https://www.random.com/5C4AV43S2VE7JBPCD6H3LZDSM4.jpg'
: request.url();
request.continue({ url });
});

mootools Request class and CORS

I'm trying to use CORS to have a script do an Ajax request to geonames.
My script calls this web service method: http://www.geonames.org/export/web-services.html#findNearby
If you check the response headers of the sample call, they include:
Access-Control-Allow-Origin: *
When I try this with mootools (version 1.4.5 just downloaded):
var urlGeonames = "http://api.geonames.org/findNearbyPlaceName";
var req = new Request({
method: 'get',
url: urlGeonames,
data: {
'lat': '89.18',
'lng': '-0.37',
'username': 'myusername',
'radius': '5'
}
}).send();
then I get an error that says :
XMLHttpRequest cannot load
http://api.geonames.org/findNearbyPlaceName?lat=89.18&lng=-0.37&username=myusername&radius=5.
Origin http://127.0.0.1 is not allowed by Access-Control-Allow-Origin.</pre>
On the other hand, when I try old style Ajax code like this:
invocation = new XMLHttpRequest();
if(invocation)
{
invocation.open('GET', urlFlickr, true);
invocation.onreadystatechange = handler;
invocation.send();
}
then it works and I get the XML response in the XHR responseXML.
I found this post A CORS POST request works from plain javascript, but why not with jQuery? that is similar. But here I'm not dealing with my server so I can only work on the javascript side.
Has anyone worked with CORS and mootools and can help on this issue ?
Thanks so much
JM
Hey man check out mootools more JSONP this will solve your problem:
http://mootools.net/docs/more/Request/Request.JSONP
Also it looks like your forgetting to ask for it in JSON format from geonames.org
Try something like:
var myJSONP = new Request.JSONP({
url: 'http://api.geonames.org/findNearbyPlaceNameJSON',
data: {
'lat': '89.18',
'lng': '-0.37',
'username': 'myusername'
},
onRequest: function(url){
// a script tag is created with a src attribute equal to url
},
onComplete: function(data){
// the request was completed.
console.log(data);
}
}).send();
Hope this helps!
The first answer on this other thread:
MooTools CORS request vs native Javascript
Might help.
Basically, the X-Requested-With header is automatically sent by the Mootools with the request, but the server either has to be configured to accept that header or you can remove it using
delete foo.headers['X-Requested-With'];
Before calling
foo.send();
To allow it by the server, you can add this to the .htaccess file of your script that gives back the JSON data:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
So yours would look like:
var myJSON = new Request({
url: 'http://api.geonames.org/findNearbyPlaceNameJSON',
data: {
'lat': '89.18',
'lng': '-0.37',
'username': 'myusername'
},
onRequest: function(url){
// a script tag is created with a src attribute equal to url
},
onComplete: function(data){
// the request was completed.
console.log(data);
}
});
delete myJSON.headers['X-Requested-With'];
myJSON.send();