简体   繁体   English

如何在氚中访问传入请求的标头?

[英]How can access the headers of an incoming request in tritium?

I would like to be able to add some logic to my tritium project based on the incoming request header. 我希望能够根据传入的请求标题为我的tritium项目添加一些逻辑。 Is it possible to access the header information and then perform match() with() logic? 是否可以访问头信息,然后使用()逻辑执行match()?

My plan is to take an existing URL (that can be accessed via a normal GET request) and give it a second mode of functionality so that it can be turned into an AJAX API. 我的计划是获取现有的URL(可以通过普通的GET请求访问)并为其提供第二种功能模式,以便将其转换为AJAX API。 When the JavaScript makes the API request, I could set a custom header flag so that the platform knows to interpret the request differently. 当JavaScript发出API请求时,我可以设置自定义标头标志,以便平台知道以不同方式解释请求。

You should be able to access headers in the incoming HTTP request using the global variable syntax. 您应该能够使用全局变量语法访问传入HTTP请求中的标头。 For example, to access the site's hostname: 例如,要访问站点的主机名:

$host
# => yourwebsite.com

I believe that most of the standard headers are accessible as global variables in Tritium. 我相信大多数标准头文件都可以作为Tritium中的全局变量访问。 However, I'm not sure if all headers are accessible as global vars. 但是,我不确定是否所有标头都可以作为全局变量访问。

Inside your project folder, on your development machine, there should be a tmp folder that contains the HTTP request/response bundles. 在项目文件夹内,在开发机器上,应该有一个包含HTTP请求/响应包的tmp文件夹。 Each bundle should be time stamped with the request's date and time. 每个捆绑包都应加盖请求的日期和时间。 I think if you peek inside one of these folders, you should see a bunch of files: 我想如果你偷看其中一个文件夹,你应该看到一堆文件:

  • incoming_request incoming_request
  • incoming_response incoming_response
  • outgoing_request outgoing_request
  • outgoing_response outgoing_response

And possibly a fifth file. 可能是第五个文件。 I can't remember if this is still the case in the current version of the platform, but there's a chance you'll find a fifth file containing the global variables that the Tritium server creates to store HTTP request header values. 我不记得在当前版本的平台中是否仍然如此,但是您可能会找到第五个文件,其中包含Tritium服务器为存储HTTP请求标头值而创建的全局变量。 So you can peek inside that file (if it exists) and find out what variable name your HTTP headers are using. 因此,您可以查看该文件(如果存在),并找出您的HTTP标头使用的变量名称。

Hope that helps! 希望有所帮助!

I'm late on this one, but I figured I would lend a hand to anyone else who needs help on this one. 我迟到了,但我想我会帮助那些需要帮助的人。

you need to create two files in your scripts directory, one called 你需要在脚本目录中创建两个文件,一个名为

request_main.ts request_main.ts

and

response_main.ts response_main.ts

You can then use things such as the parse_headers function, which iterates through the request/ response headers, depending on the file which you put the code in. 然后,您可以使用诸如parse_headers函数之类的东西,它会遍历请求/响应标头,具体取决于您放入代码的文件。

parse_headers() {  # iterate over all the incoming/outgoing headers
  log(name())      # log the name of the current cookie in the iteration
  log(value())     # log the value of the current cookie in the iteration
}

parse_headers(/Set-Cookie/) {  # iterate over the Set-Cookie headers only.
  log(this())
}

This will log all of your header names, to make modifications, you can then use "setter" functions, which you can read about here: 这将记录您的所有标题名称,以进行修改,然后您可以使用“setter”函数,您可以在这里阅读:

http://developer.moovweb.com/docs/local/configuration/headers http://developer.moovweb.com/docs/local/configuration/headers

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM