In Azure Function Java how do I set cookies using HttpResponseMessage as I don't see any methods to support that in 2.0 version.
As suggested in the official documentation , Functions should be stateless and idempotent if possible.
Associate any required state information with your data. For example, an order being processed would likely have an associated state member. A function could process an order based on that state while the function itself remains stateless.
By checking the HttpRequestMessage and the HttpRequestMessage.Builder interface, I am sorry that there is no direct method to set the cookie.
A workaround is to create your own MyHttpResponseMessage class which implements HttpResponseMessage interface.
Here is just a quick sample:
public class HttpResponseMessageBuilder {
HttpStatusType status;
Map<String,String> headers;
Object body;
private HttpResponseMessageBuilder(){};
public static HttpResponseMessageBuilder newResponseMessage(){
return new HttpResponseMessageBuilder();
}
public HttpResponseMessageBuilder status(HttpStatusType type){
status = type;
return this;
}
public HttpResponseMessageBuilder header(String key, String value){
if(headers == null) headers = new HashMap<>();
headers.put(key, value);
return this;
}
public HttpResponseMessageBuilder body(Object object){
this.body = object;
return this;
}
public HttpResponseMessageBuilder setCookie(String name,String value, String expire, String path, String domain, Boolean isSecure, Boolean isHttpOnly) {
StringBuilder sb = new StringBuilder();
sb.append(name).append("=").append(value);
if(expire != null)
sb.append(";expires=").append(expire);
if(path != null)
sb.append(";path=").append(path);
else
sb.append(";path=").append("/");
if(domain != null)
sb.append(";domain=").append(domain);
if(isSecure)
sb.append(";Secure");
if(isHttpOnly)
sb.append(";HttpOnly");
header("Set-Cookie", sb.toString());
return this;
}
public HttpResponseMessage build(){
MyHttpResponseMessage myHttpResponseMessage = new MyHttpResponseMessage();
if(status == null) status = HttpStatusType.custom(200);
myHttpResponseMessage.setStatus(status);
if(headers == null) headers = new HashMap<>();
myHttpResponseMessage.setHeaders(headers);
if(body == null) body = "";
myHttpResponseMessage.setBody(body);
return myHttpResponseMessage;
}
}
public class MyHttpResponseMessage implements HttpResponseMessage {
HttpStatusType status;
Map<String,String> headers;
Object body;
@Override
public HttpStatusType getStatus() {
return status;
}
@Override
public String getHeader(String key) {
return headers.get(key);
}
@Override
public Object getBody() {
return body;
}
protected void setStatus(HttpStatusType status) {
this.status = status;
}
protected void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
protected void setBody(Object body) {
this.body = body;
}
}
Finally, you can use it in your function:
return HttpResponseMessageBuilder.newResponseMessage()
.setCookie("SessionID", "1F5E409AE7020538130D15ACC54530F9", null, "/", ".domian.com", true, false)
.body("123456789").build();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.