我必须从用户请求中获取var http = require('http');
http.createServer(function (req, res) {
//do something
//return something
}).listen(8080);
,并将自定义信息添加到该User-agent
中,然后将其发送回去。
我有以下控制器:
User-agent
感谢您的帮助!
答案 0 :(得分:3)
您可以通过以下方式从请求中获取标头
@GetMapping("/new_order")
public String newOrder(@RequestHeader(value = "User-Agent") String userAgent, RedirectAttributes redirectAttributes, Model model) {
//...
}
要将标头发送回客户端,可以使用ResponseEntity
public ResponseEntity<String> newOrder() {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("User-Agent", "updated-Value");
return ResponseEntity.ok()
.headers(responseHeaders)
.body("New order created.");
}
答案 1 :(得分:0)
在控制器方法中插入HttpServletRequest
。从那里使用HttpServletRequest#getHeader(...)
来获取所需的标题。
@GetMapping("/new_order")
public String newOrder(RedirectAttributes redirectAttributes, HttpServletRequest request, Model model) {
String userAgent = request.getHeader("User-Agent");
if (isUserNotAuthorized()) {
return getLoginRedirectPage(redirectAttributes, "/login");
}
model.addAttribute("order", new Order());
// How to retrieve User-agent and add some custom info and return it back to the user?
return "new_order";
}