Extract Variable policy issue while extracting variable from response payload

Hi, Target endpoint we use returns response in json format but with content-type in the response header as "text/html".

So, In the post flow, under response section, we modified response content type header to "application/json" using assign message policy. After that, used extract variable policy to extract the content from response payload.

For some reason, Extract variable is not extracting the values to variables. To verify this, used another assign message policy, to add custom headers by populating with variables extracted. When this api is invoked, custom headers in the response show blank values.

we don't see this problem with extract variable when target endpoint returns json payload with content-type as application/json. But subsequently, when we use statistic policy for custom dimension for the variable , analytics always show dimension value as unknown.

Solved Solved
0 5 2,331
1 ACCEPTED SOLUTION

It works for me.

Please find attached a working API Proxy bundle.

I have 2 flows: one in which the content-type header does not get fixed (/t1), and one in which it does (/t2).

Example results:


$ curl -i https://ORG-ENV.apigee.net/soujanyaedunuri/t1
HTTP/1.1 200 OK
Date: Tue, 05 Sep 2017 23:34:52 GMT
Content-Length: 55
Connection: keep-alive
statusCode: 
Server: Apigee Router


{ "statusCode": "0000001", "statusMessage": "Success" }


$ curl -i https://ORG-ENV.apigee.net/soujanyaedunuri/t2
HTTP/1.1 200 OK
Date: Tue, 05 Sep 2017 23:34:55 GMT
Content-Length: 55
Connection: keep-alive
statusCode: 0000001
Server: Apigee Router


{ "statusCode": "0000001", "statusMessage": "Success" }



You can see the response header is correct in the latter response, incorrect in the former.

Please find the API Proxy attached here.

soujanyaedunuri-rev1-2017-09-05.zip

View solution in original post

5 REPLIES 5

******

Sample Response content returned by target endpoint:

{ "statusCode": "0000001", "statusMessage": "Success" }

Response Header content-type: Text/HTML

Here is the PostFlow content:

<PostFlow name="PostFlow">

<Request/>

<Response>

<Step> <Name>SetResponseContentType</Name> </Step>

<Step> <Name>Extract-Response-payload</Name> </Step>

<Step> <Name>SetResponseHeaders</Name> </Step>

</Response>

</PostFlow>

SetResponse Content (AssignMessage policy):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="SetResponseContentType"> <DisplayName>SetResponseContentType</DisplayName> <Properties/> <Set> <Headers> <Header name="content-type">application/json</Header> </Headers> </Set> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> <AssignTo createNew="false" transport="http" type="response"/> </AssignMessage>

Extract Variable policy content:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Response-payload"> <DisplayName>Extract Response payload</DisplayName> <Source clearPayload="false">response</Source> <JSONPayload> <Variable name="statusCode" type="string"> <JSONPath>$.statusCode</JSONPath> </Variable> </JSONPayload> <VariablePrefix>loginresponse</VariablePrefix> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </ExtractVariables>

Set custom Response headers (using assign msg policy): Tried with and without AssignTo parameter.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="SetResponseHeaders"> <DisplayName>SetResponseHeaders</DisplayName> <Properties/> <Set> <Headers> <Header name="statusCode">{loginresponse.statusCode}</Header> </Headers> </Set> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </AssignMessage>

code formatting, please. There is a button in the editor which says "code" . Click it. Paste your code into it. It makes things much easier for everyone who reads your question - the people you are asking for help.

5595-format-your-code-please.png

Thanks.

It works for me.

Please find attached a working API Proxy bundle.

I have 2 flows: one in which the content-type header does not get fixed (/t1), and one in which it does (/t2).

Example results:


$ curl -i https://ORG-ENV.apigee.net/soujanyaedunuri/t1
HTTP/1.1 200 OK
Date: Tue, 05 Sep 2017 23:34:52 GMT
Content-Length: 55
Connection: keep-alive
statusCode: 
Server: Apigee Router


{ "statusCode": "0000001", "statusMessage": "Success" }


$ curl -i https://ORG-ENV.apigee.net/soujanyaedunuri/t2
HTTP/1.1 200 OK
Date: Tue, 05 Sep 2017 23:34:55 GMT
Content-Length: 55
Connection: keep-alive
statusCode: 0000001
Server: Apigee Router


{ "statusCode": "0000001", "statusMessage": "Success" }



You can see the response header is correct in the latter response, incorrect in the former.

Please find the API Proxy attached here.

soujanyaedunuri-rev1-2017-09-05.zip

@Dino

Hi Dino,

Thanks much. I got the problem with extract variable. I had three variables Extract policy. While defining the policy, i added multiple <JSONPayload> sections for each variable instead of adding all variable under one <JSONPayload>.

Below Incorrect format used which caused the issue. Apigee didn't throw any run time error with this incorrect format. It appears that Apigee reinitializes other variables for each JSONPayload section and only the last variable value is extracted.

    <JSONPayload>
        <Variable name="statusCode" type="string">
            <JSONPath>$.statusCode</JSONPath>
        </Variable>
    </JSONPayload>
    <JSONPayload>
        <Variable name="profileType" type="string">
            <JSONPath>$.profileType</JSONPath>
        </Variable>
    </JSONPayload>

    <JSONPayload>
        <Variable name="id" type="string">
            <JSONPath>$.profile.id</JSONPath>
        </Variable>
    </JSONPayload>

Correct format:

    <JSONPayload>
        <Variable name="statusCode" type="string">
            <JSONPath>$.statusCode</JSONPath>
        </Variable>
        <Variable name="profileType" type="string">
            <JSONPath>$.profileType</JSONPath>
        </Variable>
        <Variable name="id" type="string">
            <JSONPath>$.Profile.id</JSONPath>
        </Variable>
    </JSONPayload>

Wow! Thanks for the followup note. I'm sorry to hear about that problem. I agree with you, that behavior is a bug! It seems like a big pitfall to allow it. I will file a ticket to track resolution. I'm glad you have been able to diagnose this, and move forward.

ps: thanks for formatting your code!!!! 🙂