On Rails 5, all requests includes an unique identifier accessible on application and displayed on HTTP response headers, called "X-Request-Id".
This identifier is very useful for debugging and logging, but I'm having trouble with this in a very old web client.
I tried to clear the header but it did not work.
response.headers['X-Request-Id'] = nil
How can I remove this information from headers?
You can disable it by adding this line in your config/application.rb file:
config.middleware.delete ActionDispatch::RequestId
You can disable this header information, setting by nil this request attribute.
request.request_id = nil
Related
I'm using AsyncDisplaykit in my swift application and ASNetworkImageNode as my imageview in collectionNode. I can load any external url with great performance but for my application I need to communicate with our api which requires authentication header to be sent on each GET request. How can i add authentication header in asnetworkimagenode url request or write an extension or any other workaround to achieve this?
I searched the library files and found that there is a setSharedImageManagerWith(_:URLSessionConfiguration?) in PINRemoteImageManager. One can add additional header in session configuration. So in swift 3 the code can be added in appdelegate didFinishLaunchingWithOptions as:
let config = URLSessionConfiguration.ephemeral
config.httpAdditionalHeaders = [
"clientid": "yourAdditionalHeader",
"clientkey": "yourAdditionalHeader"
] as [AnyHashable:Any]
ASPINRemoteImageDownloader.setSharedImageManagerWith(config)
Now setting the url in AsNetworkImageNode will send the url request with additional headers added to the request. This has solved my issue.
The doc of PINRemoteImageManager reads
"Sets the shared instance of PINRemoteImageManager to an instance with the supplied configuration. If configuration is nil, [NSURLSessionConfiguration ephemeralSessionConfiguration] is used. You specify a custom configuration if you need to configure timeout values, cookie policies, additional HTTP headers, etc. This method should not be used if the shared instance has already been created."
So the similar code can be used to configure timeout values, cookie policy and of course additional http headers. Hope this will help someone.
I have a grails 2.2.4 application. I wanted to enable CORS
So I installed cors plugin by having the following line in build config.
plugins {
runtime ':cors:1.1.8'
}
Then in the config.groovy
cors.headers = ['Access-Control-Allow-Origin': '*']
But after this when I run the application, CORS in not enabled. So I debugged the CORS plugin. The issue seems to be in CorsFilter class in the following method
private boolean checkOrigin(HttpServletRequest req, HttpServletResponse resp) {
String origin = req.getHeader("Origin");
if (origin == null) {
//no origin; per W3C spec, terminate further processing for both preflight and actual requests
return false;
}
The origin parameter in the above line is always null as the request does not have the parameter 'Origin'. Is there something i'm doing wrong? I'm not looking for the answer which says add a manual header with the name "Origin" since that is not exactly a proper fix
I'm quite new to CORS so appriciate the help.
In addition to Access-Control-Allow-Origin, and in addition to setting the Origin header on request, you probably need to specify these response headers as well:
Access-Control-Allow-Headers: accept
Access-Control-Allow-Headers: origin
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Method: GET
Access-Control-Allow-Method: POST
Also make sure you respond to HTTP OPTIONS requests with these headers and a blank 200 OK response.
For now, let's assume that RestClient is sending the Origin header properly. It may still be getting stripped by your application. You can prevent this using the Access-Control-Allow-Headers: Origin header.
Most of the problems I have had with my web services is that the right headers are being sent, but they are stripped from the message by my web server. So I tend to adopt a shotgun approach of "allow everything" and then one by one remove what I don't need. My allow-headers header usually is pretty long and I end up having to include stuff like Content-Type, X-Requested-With and other junk before my requests will finally go through.
I further recommend that you test using something besides RestClient, if only as a sanity check. I use Postman, a free Chrome app, for all my messaging tests. It looks to me like the problem is with RestClient not sending the proper Origin header.
If I load a section of my website with Net::HTTP in Rails, will this get loaded every time or will it get cached along with the rest of the footer?
EDIT: I mean the rest of the footer is currently cached. Would the Net:HTTP results, which get rendered inside the footer, also become cached? I would like it to reload the results every time.
No, Net::HTTP will not cache anything for you. You will have to implement caching, or use a gem that does it for you. But depending on what you do with Rails, Rails can do it - look into fragment caching.
Doesn't look like it does, at least not by default as of 2011. There's also a segment in the net/http.rb file in the ruby source that has the following code commented out:
# The following example performs a conditional GET using the
# If-Modified-Since header. If the files has not been modified since the
# time in the header a Not Modified response will be returned. See RFC 2616
# section 9.3 for further details.
#
uri = URI('http://example.com/cached_response')
file = File.stat 'cached_response'
req = Net::HTTP::Get.new(uri)
req['If-Modified-Since'] = file.mtime.rfc2822
res = Net::HTTP.start(uri.hostname, uri.port) {|http|
http.request(req)
}
open 'cached_response', 'w' do |io|
io.write res.body
end if res.is_a?(Net::HTTPSuccess)
The source file is dated July 9. Hope that helps.
I am trying to added a non-english language for a test using capybara and poltergeist. I have tried:
page.driver.headers = { 'HTTP_ACCEPT_LANGUAGE' => 'pt-BR' }
But that is not working. On the server side, 'HTTP_ACCEPT_LANGUAGE' is always 'en-US'. I have even tried adding another arbitrary header but that isn't coming through on the server side. It seems like poltergeist's header setting doesn't seem to work.
I expected that the right header to set was the same as retrieving in rails but HTTP_ACCEPT_LANGUAGE is not a valid http header (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). It is specific to rails.
I needed to do:
page.driver.headers = { 'ACCEPT-LANGUAGE' => 'pt-BR' }
I want to create a proxy controller in grails, something that just takes whatever is passed in based on a url mapping, records what was asked for, sends the request to another server, records the response, and send the response back to the browser.
I'm having trouble with when the request has an odd file extension (.gif) or no file extension (/xxx?sdcscd)
My url mapping is:
"/proxy/$target**"
and I've attempted (per an answer to another question):
def targetURL = params.target
if (!FilenameUtils.getExtension(targetURL) && request.format) {
targetURL += ".${response.format}"
}
but this usually appends .html and never the .gif or ?csdcsd
Not sure what to do as I might just write the thing in straight Java
Actually, the real answer was sitting in the post you linked to previously all along, by Peter Ledbrook:
Disable file extension truncation by adding this line to grails-app/conf/Config.groovy:
grails.mime.file.extensions = false
This will disable the usage of file extensions for format, but will leave the file extension on params.target. You can completely ignore response.format!