我有一个用Jolie编写的服务,我想根据请求提取http标头。以同样的方式可以打印出request.id,我想打印标题。在代码中尝试使用粗体字母。代码如下:
execution { concurrent }
inputPort UserDB_Service {
Location: "socket://localhost:8002/"
Protocol: http { .format = "json"}
Interfaces: Users, ShutdownInterface, ConnectionPool
}
outputPort DB_Connector {
Location: "socket://localhost:1000/"
Protocol: sodep
Interfaces: ConnectionPool
}
init
{
connectionConfigInfo@DB_Connector()(connectionInfo);
connect@Database(connectionInfo)()
}
main
{
//Example: http://localhost:8002/retrieve?id=1
[ retrieve(request)(response) {
query@Database(
"select * from users where user_id=:id" {
.id = request.id
}
)(sqlResponse);
println@Console( "You have requested the user_id: " + request.id)();
**println@Console( "Request Headers: " + response.format)();**
if (#sqlResponse.row == 1) {
response -> sqlResponse.row[0]
}
} ]
}
感谢您的帮助。
答案 0 :(得分:1)
我不明白您是否知道要在入站请求中拥有哪些标头,或者您是否只想打印整个http消息以进行调试。在两种情况下都很快,我报告了两种解决方案:)
在第一种情况下,您可以为inputPort设置http协议的headers
参数,以在请求消息中包含特定标头的内容,例如,
http {
.headers.format = "format";
}
然后你可以用通常的方式检查值
println@Console( request.format )()
在第二种情况下,您可以使用
http {
.debug = true;
.debug.showContent = true
}
查看所有http请求和响应及其正文的日志。
有关协议的更多信息,特别是http协议,请参阅Jolie网站的文档。
答案 1 :(得分:0)
我再次把输出放在这里。我想知道是否有可能提取“iv-user:g47257”标题,我已经使用Fiddler注入了它。再次感谢您的帮助。 标题是这样的(更好的格式)。
INFO: [UserDB_crud.ol] [HTTP debug] Receiving:
HTTP Code: 0
Resource: /retrieve?id=1
--> Header properties
iv-user: g47257
accept-language: en-US,en;q=0.8,da;q=0.6,es;q=0.4
host: localhost:8002
upgrade-insecure-requests: 1
connection: keep-alive
cache-control: max-age=0
accept-encoding: gzip, deflate, sdch
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp
,*/*;q=0.8
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTM
L, like Gecko) Chrome/48.0.2564.116 Safari/537.36
You have requested the user_id: 1
mar. 10, 2016 2:30:44 PM jolie.Interpreter logInfo
INFO: [UserDB_crud.ol] [HTTP debug] Sending:
HTTP/1.1 200 OK
Server: Jolie
X-Jolie-MessageID: 0
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Content-Length: 72
?V*H,..?/JQ?R*I-.Q?Q*-N-??♦
↑?(?%?"dRs‼3s?\►?????T♂ %??WE
mar. 10, 2016 2:30:44 PM jolie.Interpreter logInfo
INFO: [UserDB_crud.ol] [HTTP debug] Receiving:
HTTP Code: 0
Resource: /favicon.ico
--> Header properties
iv-user: g47257
referer: http://localhost:8002/retrieve?id=1
accept-language: en-US,en;q=0.8,da;q=0.6,es;q=0.4
host: localhost:8002
connection: keep-alive
cache-control: no-cache
pragma: no-cache
accept-encoding: gzip, deflate, sdch
user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTM
L, like Gecko) Chrome/48.0.2564.116 Safari/537.36
accept: */*
mar. 10, 2016 2:30:44 PM jolie.Interpreter logWarning
WARNING: [UserDB_crud.ol] Received a message for operation favicon.ico, not specified in the input port at the receiving service. Sending IOException to the caller.
mar. 10, 2016 2:30:44 PM jolie.Interpreter logInfo
INFO: [UserDB_crud.ol] [HTTP debug] Sending:
HTTP/1.1 200 OK
Server: Jolie
X-Jolie-MessageID: 0
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Content-Length: 102
?VJ-*?/R??V?M-.NLOU?R??w?HN-(???S?QJ?O☺?→←↓↑↑?(?$?$???%?d?(?↨?▬%?¶Z)?%?e&???☺ ??Z ?yd?Y
答案 2 :(得分:0)
我在这里重新发表了我的最后评论,因为其他人也遇到了Efrin遇到的同样困难,但可能会错过我作为评论发表的解决方案。
您可以检查HTTP请求的标头,如下面的代码所示
include "console.iol"
inputPort Me {
Location: "socket://localhost:8000"
Protocol: http { .headers.iv_user = "ivUser" }
RequestResponse: myRequest
}
main {
myRequest( request )(){ println@Console( request.ivUser )() }
}
请记住,如documentation中所述,Jolie http.headers
参数将-
映射到带有_
的标头名称中,例如,在您的情况下,标头{{1} }成为Jolie HTTP协议参数中的iv-user
。
除了Jolie文档中的描述和代码外,您还可以在Montelie https://doi.org/10.1016/j.scico.2016.05.002撰写的演示文稿中找到更多示例以及有关HTTP协议如何在Jolie中进行更详尽的解释。