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
Related
My web app generates a UUIDv4 for every new 'post', and each post has its own URL like /posts/<uuid>. I'm not able to predict what uuid gets generated, and therefore I'm unable to go back to a specific post that was created earlier during testing.
According to the docs, cy.url() returns the URL as a string. I tried saving the URL using a .as(), but it didn't work:
cy.url().as('postUrl');
// go somewhere else
cy.visit('#postUrl');
// ends up visiting `localhost:3000/#postUrl`
I saw in another SO question that I should use .then on cy.url(), but that didn't work either:
cy.url().then(url => url).as('postUrl');
// go somewhere else
cy.visit('#postUrl');
How do I save the current URL for use later?
Found the answer buried in later pages of a google search. In order to use the saved URL, use cy.get('#postUrl') and call a .then with a callback that visits that url.
cy.url().as('postUrl');
// go somewhere else
cy.get('#postUrl').then(url => {
cy.visit(url);
}
var currentUrl=''
cy.url().then(url => {
currentUrl = url;
});
cy.visit(currentUrl)
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.
I need to get the permission to access to specific URL, but before that I need to get the responseKey, and the way to get the responseKey is to access to this URL in the browser: https://api.getgo.com/oauth/v2/authorize?client_id={consumerKey}&response_type=code. After accessing
it contains the responseKey what I need. It will look something like: http://example.com/oauthreturn/?code={responseKey}. The question is how I can get the responseKey in code in google-apps-scrpits.
This what i wrote:
function myFunction() {
var client_id='xxxxxxxxxxxxxxxxx'
var url ="https://api.getgo.com/oauth/v2/authorize?client_id="+client_id+"&response_type=code";
var resp = UrlFetchApp.fetch(url)
}
but I get a HTML code response
In your code var resp = UrlFetchApp.fetch(url) will create a HTTPResponse object. First, you will want to know if the response code is 200 OK. To gather that information you should use getResponseCode() and check if it equals to 200. If that is the case, then you must parse the code. To do that you can first use getContentText() and after that parse(). One example implementation, that is compatible with your code, can be this one:
if (resp.getResponseCode() == 200) {
var results = JSON.parse(resp.getContentText());
}
You can then access to the data referring to that variable and the name of the parameter. For example, you could use results.code, results.token… If this explanation doesn't clarify your question, please ask me again for further help.
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.
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.