Does the IBM Worklight HTTP Adapter send/support s

2019-09-02 18:12发布

问题:

Do IBM Worklight HTTP Adapters (in 6.1) send a User-Agent header by default when invoking a back-end service using WL.Server.invokeHttp? What is it's value? Assuming the answer is no, can we add one?

回答1:

In the adapter you can get the user agent the client sent like this:

var clientRequest = WL.Server.getClientRequest();
var userAgent = clientRequest.getHeader("User-Agent");

If you then want to pass this header along to a backend service:

var input = {
    method :'get',
    path : 'your/path',
    headers: {
        "User-Agent" : userAgent,
    }
};

var result=WL.Server.invokeHttp(input);


回答2:

When you invoke an adapter procedure, you can inspect the network using a tool such as Wireshark. There you will see that a User-Agent header is sent. This header is automatically added by the underlying Apache HTTPClient.

That said, you can add your own headers. Per the user documentation for WL.Server.invokeHttp:

Parameters:  
options - The invokeHttp function accepts the following JSON block of parameters:  
...  
...  
...  
headers. Optional. Defines the headers for the HTTP request.

For example:

var input = {
        method : 'get',
        headers: {foo: 'bar'},
        path : '/mypath'
};  
return WL.Server.invokeHttp(input);

As for its value, it may not have any value for you. It is just part of the standard.
See here for more (or google for additional information): HTTP request header: UserAgent variable