Invoke-WebRequest: Error fetching Ajax content - exception

I have a problem, that no one on google seemed to have before:
I'm trying to fetch the content of a webpage in PowerShell 3 with
Invoke-WebRequest -Uri $myUri -DisableKeepAlive
but I get a pop-up everytime that says Error fetching Ajax content.<br/>Server Response: undefined. I click OK and my script continues without complaint, but it pauses the execution, which is a problem.
I tried to run it with PowerShell.exe -windowstyle hidden {myScript.ps1}, and it suppresses the pop-up, but script execution stops, when it normally would appear.
I tried a Try Catch block, but it doesn't seem to get it.
It would be great if someone here had an idea.

Try using the -UseBasicParsing switch on Invoke-WebRequest to avoid DOM processing of the returned HTML.

Related

HTTP request error from running Postman Collection tests with Newman?

I've been using the new commandline for Postman, Newman, and have been attempting to run Collection tests that work fine when I pass them through the packaged app Jetpacks add-on, but do not run properly in the commandline. Although the json Collection file that I am passing does contain the proper header declarations, I don't have any other clues at this point, so I suspect that this may be an HTTP header issue. But I am not sure exactly what is wrong, as I am rather new to using Postman.
The tests that I'm trying to run are on some calls to an ASP.Net web API, very simple server response-checking one-line javascript tests like the ones in this tutorial.
A sample line that I enter into the console:
$ newman -c collectionfile.json -e environmentfile.json -n 5
achieves such a result:
RequestError: [token] terminated. Error: undefined
Any suggestions/help would be appreciated.
I ran into this problem as well and spent quite a few hours trying to figure it out. Eventually I realized that an address such as "www.google.com" will work in the chrome plugin UI, but not in Newman. For it to work in Newman, the address must be "https://www.google.com". Also, make sure that you have your data file (if you are using variables like {{url}}) set up correctly. You can use "-d dataFile.json" to define a data file. More information on that here.

CGI scripts for bash commands

I'm kinda new to all this, so tell me if I'm doing something totally wrong.
I'm doing some gpio stuff with my raspberry pi, and at the moment i'm making something so the gpio pins can be controlled via a web interface. One of the ways I'm doing this is using bash CGI scripts to control the pins, and executing them from the browser.
So far the only way I can get this to work involves the browser loading the page ".../cgi-bin/gpio1.cgi" etc, which contains the code:
#!/bin/bash
echo "Content-type: text/html"
echo ""
...gpio stuff...
This works, but the browser navigates to the blank page created by this script.
Is there a way of executing these scripts without leaving the webpage, and so the scripts aren't writing HTML but instead focusing on the actual gpio stuff?
Thanks
Try this:
#!/bin/bash
echo "Status: 204 No Content"
...gpio stuff...
HTTP responses must start with a status line; webservers will normally add status "200 Ok" if the CGI doesn't specify one. That status must be accompanied by response body, which will form the new web page.
The status you want is 204, which indicates that the request was satisfied but that there is no response and the browser should stay on the same page. Normally, this would be a response to a POST request, not a GET request, but it should work anyway. Since a 204 response does not require a response body (in fact, it doesn't permit one), it should not be necessary to output a blank line following the status line, but you might need one if the script takes a long time to run.

Extra /**/ in facebook profile picture json

Im trying to retrieve the URL for a facebook users profile pic. The call returns the JSON I expect as per http://developers.facebook.com/docs/reference/api/using-pictures/#json with one problem - if I specify callback=profile I get a /**/ at the start of the line. Unfortunately this is causing the JSON parser (lift-json) I am using to throw an exception.
Is there a way to stop the /**/ being returned? It seems odd but I cant see any reason its happening and thus see no way to turn it off.
The problem I am facing is shown below using curl.
andrews-MacBook-Pro:~ doctorb$ curl "http://graph.facebook.com/582931709/picture?redirect=false&type=normal"
{"data":{"url":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-ash4\/274518_582931709_901337157_s.jpg","is_silhouette":false}}
andrews-MacBook-Pro:~ doctorb$ curl "http://graph.facebook.com/582931709/picture/redirect=false&type=normal&callback=profile"
/**/ profile({"data":{"url":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-ash4\/274518_582931709_901337157_s.jpg","is_silhouette":false}});
Your first request is for simple JSON and your second request is for a JSONP formatted response.
Facebook adds a comment at the beginning of their JSONP response to prevent a vulnerability called "JSONP hijacking" in the web browser. As any requests to graph.facebook.com will include Facebook cookies, the comment will prevent malicious websites from simply including the request in a script tag and surreptitiously acting on behalf of the authenticated user.
If you want to use the JSONP request, instead of the simple JSON request, you will need to strip out the comment at the beginning of the response before passing it along to your JavaScript parser.

Contact API directly from URL in browser

I am trying to understand how POST and/or GET methods work in terms of the actual browser.
I am attempting to contact an API which requires API key, method you wish to use on their side, and an IP address at the minimum.
My original idea was to do something like this:
I feel like I'm on the right track, it does something and gets an error as opposed to telling me the page does not exist. I'm expecting either JSON or XML in response as the API supports both but instead I get this error:
This page contains the following errors:
error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.
Upon studying the documentation of the API more, I found something saying that methods are called using HTML form application/x-www-form-urlencoded and the resuource models are given as form elements.
I tried researching what that means to see what the problem was and found this site http://www.w3.org/TR/xforms11/ but I'm still unclear.
Ideas?
It seems to mean that the application is expecting a POST method but you're doing a request with a GET method (when you use the querystrings).
Since you can't just do browser requests using POST using the address bar, you may need to:
Construct a simple JS function that does a xmlhttprequest request using that method instead, and running it from the console;
Create a simple HTML page that automates the above process, allowing you do make POST calls;
Using CURL instead, which is a great tool for testing those kinds of requests.

Exceptions: redirect or render?

I'm trying to standardize the way I handle exceptions in my web application (homemade framework) but I'm not certain of the "correct" way to handle various situations. I'm wondering if there is a best practice from UI/ user-friendly point of view.
User logs into application and opens two tabs showing the same screen. On one tab they issue a delete command on object FOO. Then, in the other tab they then click the edit command on FOO (which no longer exists); e.g. a GET request for editObject.php?object_id=FOO. What should I do when they issue the edit request for this nonexistent object?
-Currently I am redirecting these "missing" objects to the previous page with an error message like "object does not exist".
User issues a GET request to search for Objects with color=Red, e.g. searchObjects.php?color=Red. The query returning these results blew up because somebody dropped the OBJECTS table. This is an unexpected exception and isn't quite the same as 1).
-Currently I am redirecting to errorPage.php with a message "Unexpected error"
In general, what should I do if GET/POST parameters that should be there are instead mysteriously missing. Perhaps somebody is trying to inject something?
-Currently I am treating these the same as 2)
What should I be doing in each of the above 3 cases?
Render a "Object does not exist" view at the url editObject.php?object_id=FOO
Redirect to a controller that displays an error view: header('Location: errorPage.php')
Serve a 404: not sure of the syntax for doing this in PHP/Apache
Other
I'd say render it and serve a 404. That way, the user has the chance to see where they went wrong in the URL, or copy & paste it. If you redirect to a generic error page, they don't have that chance.
The PHP way to serve a 404 is
header("HTTP/1.0 404 not found");