Sending a list of HttpRequest(s) to the same URL - dart

When I write this Dart code :
for(int i=0;i<nbAleas;i++){
HttpRequest request=new HttpRequest();
// end of request event
request.onReadyStateChange.listen((_) {
if (request.readyState == HttpRequest.DONE &&
(request.status == 200 || request.status == 0)) {
handleResponse(request.responseText);
}
});
// error event
request.onError.listen((Object error)=>handleError(error));
// method and url
request.open("GET", urlServiceRest);
// send the request
request.send();
}
the request is sent only once. I verified it on the server. If I modify the opening like this :
request.open("GET", urlServiceRest, async:false);
it works. Why should the requests be synchronous ?
Also, the above requests are made to the same URL with the same parameters, for example "localhost:8080/random/10/20". If I send to async requests to this URL, only one is sent as said above. If for the second request, I change some parameters "localhost:8080/random/11/21", the two async requests are sent.
Can anyone explain this strange behavior ? Thanks in advance.

The same GET requests will definitely be a candidate for caching by the browser. In addition to appending some random junk to the URL, you could try switching to POST requests; which are not cacheable unless the response includes appropriate Cache-Control or Expires header fields.

Related

Is there a way to get the URL after a redirect in postman?

I'm working with an API, which after filling out the log in form on their website, redirects back to our website, with a unique code at the end of the URL.
Example URL after redirect:
https://www.mywebsite.com/?code=12431453154545
I have been unable to find a way of viewing this URL in Postman.
Ideally I need to be able to work with that URL to extract the code and store it as a variable.
Any help will be muchly appreciated. I've been trying this all day :( .
When you turn off following redirects in Postman settings, you will be able to inspect 3xx HTTP response which will contain Location header with the URL you want to read.
const url = require("url");
var location = pm.response.headers.get("location");
if (typeof location !== typeof undefined) {
var redirectUrl = url.parse(location, true);
query = redirectUrl.query;
if ("code" in query) {
pm.globals.set("code", query.code);
console.log(pm.globals.get("code"));
}
}
Note that this solution will not work when multiple subsequent redirects happen as you will be inspecting only the first 3xx response. You could solve this by following redirects manually and sending your own requests from Postman script as described in Postman manual.

Workbox : find a request is XHR or fetch

Is there a way to find if a request is XHR or fetch while using Workbox.
const matchCb = ({url, event}) => {
if(event.type === 'xhr')
{
return true;
}
return false;
};
workbox.routing.registerRoute(
matchCb,
workbox.strategies.networkOnly()
);
I have put a check so that the above route is used only for XHR calls.
Although network Tab of the browser shows a certain request to be of the type xhr it is coming out to be fetch on debugging the above code . Am i doing something wrong? Is there some other way to check it?
There's no way to determine that from within Workbox or inside of the service worker. (I'm also not sure why you would want to?)
One thing that you can do, however, is add in an extra request header when you make your request, and then check for that header inside of your service worker. If it's really important for you to distinguish between requests that originated via XHR and va fetch(), you could use the header for that.
Inside your web app:
const request = new Request('/api', {headers: {'X-Source': 'fetch'}});
const response = await fetch(request);
Inside your service worker, using Workbox:
workbox.routing.registerRoute(
// This matchCallback will only be true if the request
// has an X-Source header set to 'fetch':
({event}) => event.request.headers.get('X-Source') === 'fetch',
workbox.strategies.networkOnly()
);
Note that if you're making a cors request, you may need to delete that X-Source request header before sending it to the network, since extra request headers can trigger CORS preflight checks.

How to find out the validity of a URL in Flutter?

I don't know of the title is completely appropriate, but here's what I actually want to do. I have urls which point to mp3 files, now some are valid and and some are not, as of now, so before I render the controls for playback of those audio files in my UI, I want to check whether the URL will give me the mp3 or result in a 404. How can I do that?
Make an HTTP request and check the response code
final response =
await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'));
if (response.statusCode == 200) {
See https://flutter.io/docs/cookbook/networking/fetch-data for more details.
A HEAD request will be more efficient though when you only want to check if the URL is valid because it doesn't actually download content.
final response =
await http.head(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'));
if (response.statusCode == 200) {
See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD

save a post request with fiddler script OnBeforeRequest event?

For testing purpose
i would use this code to pause a post request with customizerules.js file
static function OnBeforeRequest(oSession: Session)
{
if (oSession.HTTPMethodIs("POST") && (oSession.utilFindInRequest("mykeyword", true) > -1)){
oSession["x-breakrequest"] = "keyword";
// i need to save the post request to file here
}
}
Is there anything
It's not clear what you're asking. Your x-breakrequest Session flag suffices to set a request breakpoint for the specified request.
If you'd like to further save the request body to a file, you could do something like:
File.WriteAllBytes("C:\\temp\\" + oSession.id.ToString() + " .txt",
oSession.requestBodyBytes);
Keep in mind that this saves the original request body, not including any modifications the user may have made to the body while the request was paused at the breakpoint.

Https request with XMLHttpRequest

Im able to do Http requests (POST/GET) with XMLHttpRequest.
I'm asking how to do requests with URLs like "https://www.gmail.com"
I was trying something like this but the status code is 0
var http = new XMLHttpRequest();
var url = "https://www.gmail.com";
http.open("GET", url);
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
//alert(http.responseText);
print("ok")
}else{
print("cannot connect")
print("code:" + http.status)
print(http.responseText)
}
}
http.send(null);
I get always "cannot connect" "code" 0 and nothing as response
Any idea?
This is going to fail for two reasons:
1) The url "https://www.gmail.com" actually tries to redirect you to "https://mail.google.com/mail/", which in turn will try to redirect you to a login page. This redirect is not being listed as an error
2) However more importantly, you cannot make XMLHttpRequests to a different domain, unless that domain supports CORS (http://www.html5rocks.com/en/tutorials/cors/). GMail does not support CORS, so this request will not work.

Resources