Setting header value within Java callout.

We have an API which doesn't actually have a southbound target, but instead contains a Java callout which creates a url which we want to return to the user as a header value. I have tried setting it within the Java using:

messageContext.getMessage().setHeader("Location", url);

which doesn't seem to work. I am also setting it to a variable:

messageContext.setVariable("locationHeader", url);

which I can see in trace as containing the correct value. I have attempted to use this variable (locationHeader) to then set it by using an AssignMessage policy (which we do often), but that doesn't seem to be setting the header, either, although I can see the policy firing and the variable value is set at that stage.

Solved Solved
1 5 2,146
2 ACCEPTED SOLUTIONS

The header elements will be in the format 'message.header.<headerElementName>'. If you want to set/get the header elements please use above format.

Format: messageContext.setVariable("message.header.<headerElementName>", <VariablreName>);

View solution in original post

Please refer the apigee documentation, and through this you can set headers in response as well:

messgeContext.getResponseMessage().setHeader(key,value)

View solution in original post

5 REPLIES 5

I do a bunch of Java callouts, and can test this out. In the past , I have used Java callouts to set headers. Seems like there might be something amiss.

The Java callout - it runs in the request or the response flow? If you set a Location header in the request flow, I'm not sure it will propagate to the response.

Also you can try setting a variable in the message context, like so:

messageContext.setVariable("message.header.Location", "http://example.com");

I will let you know what I find in my tests. I have some other things going on, so I may not be able to test it immediately. Today, though.

Hi @Sean Case

In my tests, either variation works.

    public ExecutionResult execute (MessageContext messageContext, ExecutionContext execContext) {
        try {
            String hdrName = getHeaderName(messageContext);
            String hdrValue = getHeaderValue(messageContext);
            String variation = getVariation(messageContext);
            if (variation!=null && variation.equals("1")) {
                messageContext.getMessage().setHeader(hdrName, hdrValue);
            }
            else {
                messageContext.setVariable("message.header." + hdrName, hdrValue);
            }
        }
        catch (java.lang.Exception exc1) {
            if (getDebug()) {
                System.out.println(ExceptionUtils.getStackTrace(exc1));
            }
            String error = exc1.toString();
            messageContext.setVariable(varName("exception"), error);
            int ch = error.lastIndexOf(':');
            if (ch >= 0) {
                messageContext.setVariable(varName("error"), error.substring(ch+2).trim());
            }
            else {
                messageContext.setVariable(varName("error"), error);
            }
            messageContext.setVariable(varName("stacktrace"), ExceptionUtils.getStackTrace(exc1));
            return ExecutionResult.ABORT;
        }
        return ExecutionResult.SUCCESS;
    }


Variation 1 uses messageContext.getMessage().setHeader(). Variation 2 uses messageContext.setVariable(). Both work.

Attached please find full source code and a working example API proxy.

apigee-edge-java-callout-set-header.zip

Thank you all. I had read the documentation and had tried these variations. I think that there were also deployment issues which were not uploading the changes so I didn't see the changes take effect. I have fixed the clean step on our deploy and it is working now. Thanks again!

The header elements will be in the format 'message.header.<headerElementName>'. If you want to set/get the header elements please use above format.

Format: messageContext.setVariable("message.header.<headerElementName>", <VariablreName>);

Please refer the apigee documentation, and through this you can set headers in response as well:

messgeContext.getResponseMessage().setHeader(key,value)