I'm attempting to debug my web application with FireFox3. However, when a JSON feed comes from my application, Firefox wants to open up the "application/json" in a new program. Is there a way to configure FireFox3 to handle JSON like regular text files and open up the JSON in the current tab?
Thanks.
The JSONView Firefox extension is really nice.
It formats, highlights, etc...
The only drawback is that it requires the mime type to be set to "application/json".
But it is not really a drawback for you, because based on your "answer" (which shouldn't be an answer) your problem is that the mime type is "application/json" and as a result Firefox doesn't know what to do with it and downloads it instead of displaying.
(source: mozilla.net)
Try the Open in browser extension.
[edit 30.05.2010 - updated the link]
I would look into the preferences > applications list. What application is targeted for "application/*" ?
Apart from that, are you using FireBug? Absolutely essential, since you can look at the headers and response content within the network view.
Consider using a MIME type of text/javascript instead of application/json
I would just use Firebug - it'll let you drill down into a JSON object on its own, along with its other hundred useful features.
What is the content-type of the Json feed. Sounds like it may be some sort of application instead of text.
Change the content type of the feed to something that is text based and FireFox will no longer try to open it in another program.
Having JSON sent with an application/json mimetype is correct and changing that would be wrong.
text/javascript is considered obsolete.
This is a bit of an old question, but I discovered that Rails' respond_to method (at least as of 3.1) can be persuaded to render in a particular format by adding the query param 'format' to the resource in question. For example:
In the controller:
def show
#object = Object.find(params[:id])
respond_to do |format|
format.html
format.json { render json: #object }
end
end
In the browser:
/object/1 # => renders as html
/object/1?format=json # => renders as json
/object/1.json # => also renders as json
No change to the rails app is necessary to cause this to happen. It's Like Magic.
Related
Essentially, I want all of my responses returned in JSON by default, searched for an answer and stumbled upon this discussion: ServiceStack default format
What I tried:
Setting DefaultContentType to JSON and disabling Feature.Html --> works for responses, but breaks SwaggerUI (error on page render)
Only setting DefaultContentType to JSON --> doesn't break SwaggerUI, but making requests to my services from browser returns HTML (which makes sense because browsers usually the Accept header to receive html or xml, but I want to default to JSON)
That said, is there any way to only (and safely) enable Feature.Html for SwaggerUI? Maybe using PreRequestFilters?
The issue is removing the HTML Format essentially removes HTML ContentType from consideration, but I've changed it to preserve the Content Type if the Service returns a raw HTML string in this commit where the Swagger UI can return HTML pages even if the HTML Format is disabled.
This change is available from v5.4.1 that's now available on MyGet.
An alternative is to leave the HTML Format enabled but use a request filter to change the Content Type to JSON where it's HTML for all Requests you want to do this for, e.g:
PreRequestFilters.Add((req, res) => {
if (req.ResponseContentType.Matches(MimeTypes.Html) && !req.PathInfo.StartsWith("/swagger-ui"))
req.ResponseContentType = MimeTypes.Json;
});
I'm trying to write a spider that will automatically log in to this website. However, when I try using scrapy.FormRequest.from_response in the shell I get the error:
No <form> element found in <200 https://www.athletic.net/account/login/?ReturnUrl=%2Fdefault.aspx>
I can definitely see the form when I inspect element on the site, but it just did not show up in Scrapy when I tried finding it using response.xpath() either. Is it possible for the form content to be hidden from my spider somehow? If so, how do I fix it?
The form is created using Javascript, it's not part of the static HTML source code. Scrapy does not parse Javascript, thus it cannot be found.
The relevant part of the static HTML (where they inject the form using Javascript) is:
<div ng-controller="AppCtrl as appC" class="m-auto pt-3 pb-5 container" style="max-width: 425px;">
<section ui-view></section>
</div>
To find issues like this, I would either:
compare the source code from "View Source Code" and "Inspect" to each other
browse the web page with a browser without Javascript (when I develop scrapers I usually have one browser with Javascript for research and documentations and another one for checking web pages without Javascript)
In this case, you have to manually create your FormRequest for this web page. I was not able to spot any form of CSRF protection on their form, so it might be as simple as:
FormRequest(url='https://www.athletic.net/account/auth.ashx',
formdata={"e": "foo#example.com", "pw": "secret"})
However, I think you cannot use formdata, but instead they expect you to send JSON. Not sure if FormRequest can handle this, I guess you just want to use a standard Request.
Since they heavily use Javascript on their front end, you cannot use the source code of the page to find these parameters either. Instead, I used the developer console of my browser and checked the request/response that happened when I tried to login with invalid credentials.
This gave me:
General:
Request URL: https://www.athletic.net/account/auth.ashx
[...]
Request Payload:
{e: "foo#example.com", pw: "secret"}
Scrapy has a JsonRequest class to help with posting JSON. See here [https://docs.scrapy.org/en/latest/topics/request-response.html]
So something like the below should work
data = {"password": "pword", "username": "user"}
# JSON POST to API login URL
return JsonRequest(
url=url,
callback=self.after_login,
data=data,
)
My application generates a dynamic link to any PDF files that are associated with a product. The link is presented like this:
Brochure
If the user right-clicks and selects "Download Linked File As" (or its equivalent), the file is presented with a ".pdf.png" extension in Google Chrome and Safari. Firefox works appropriately, not sure about Internet Explorer.
I want Firefox and Chrome to know that it is a PDF. Because obviously users are going to try to download these, they are going to save it with the wrong extension, and they won't be able to open the file.
Assuming you are using "send_data" from within a rails controller to serve the file might I suggest:
send_data(
data,
:filename => "filename.pdf",
:disposition => "attachment",
:type => 'application/pdf'
)
Where "data" is the contents of the PDF.
For more information checkout the following link:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
send proper headers in some scripting lang like php or user .htaccess
<Files *.pdf>
ForceType application/pdf
Header set Content-Disposition attachment
</Files>
Have you heard of the content-disposition header? It allows you to tell the browser to ask the user what to do with the file, rather than try and handle it by itself. I don't think it is part of the HTTP spec, but it is documented by the IETF under RFC 2183.
You should be able to use whatever language you are using to alter the HTTP headers before they go to the client. The header you add will look something like this:
Content-Disposition: attachment; filename=filename.pdf
You might also need a Content-Type header:
Content-Type: application/pdf
i wonder if there is some way to do something like that:
If im on a specific site i want that some of javascript files to be loaded directly from my computer (f.e. file:///c:/test.js), not from the server.
For that i was thinking if there is a possibility to make an extension which could change HTML code in a response which browser gets right before displaying it. So whole process should look like that:
request is made
browser gets response from server
#response is changed# - this is the part when extension comes in
browser parse changed response and display page with that new response.
It doesnt even have to be a Chrome extension anyway. It should just do the job described above. It can block original file and serve another one (DNS/proxy?) or filter whole HTTP traffic in my computer and replace specific code to another one of matched response.
You can use the WebRequest API to achieve that. For example, you can add a onBeforeRequest listener and redirect some requests:
chrome.webRequest.onBeforeRequest.addListener(function(details)
{
var responseData = "<div>Some text</div>"
return {redirectUrl: "data:text/html," + encodeURIComponent(responseData)};
}, {urls: ["https://www.google.com/"]}, ["blocking"]);
This will display a <div> element with the text "some text" instead of the Google homepage. Note that you can only redirect to URLs that the web server itself is allowed to redirect to. This means that redirecting to file:/// URLs is not possible, and you can only redirect to files inside your extension if these are web accessible. data: and http: URLs work fine however.
In Windows you can use the Proxomitron (proxomitron.info) which is a local proxy that can intercept any page or file being loading into your browser and change it using regular expressions (no DOM parsing) however you want, before it is rendered by the browser.
I have an application which is sending me JSON object. Now I want to see how this json data structure in javascript alert something like this. How can I do that?
You can't even develop in Chrome/firefox ? I would look at changing this, it would be like coding with one hand tied behind your back. My preferred method is using Chrome dev toolbar, but here are some other ways.
You can debug using a browser based debugger, for ie .
You could use the JSON.stringifiy and alert the output,the code is here
And there are these viewers as well http://jsonviewer.stack.hu/ and jollydroll
And finally you can loop through the different object properties like so :
for(var propertyName in yourJSON){
//will loop through the different elements in your json
alert(yourJSON[propertyName]); //will output the valueof each element
alert(propertyName); //will output name of each element
}
You could throw the JSON into Firebug's console (or Chrome's developer console?) and get a pretty-print view of it.
easiest would be to use alert(json) or console.log(json) - available with development tools
In IE8, Tools->Developer Tools (F12), Script.
Set a breakpoint and examine the JSON result in Locals? It won't be 'structured', but should be readable if it's not huge