Unable to make HTTP request via httpClient or ServiceCallout policy

Not applicable

I have a requirement wherein I am supposed to make a request to an external API to fetch a token. Having fetched the token, I am supposed to set that value in the request header and pass it to back end.

I have tried to make that call using httpClient and ServiceCallout function, but unable to execute it. I have to send a JSON payload to the API along with Header Information. While I try to execute ServiceCallout, it gives me the error 'Unresolved variable'. Can anyone give me a practical example of how to execute a POST request sending a JSON payload or execute the same functionality using a ServiceCallout??

Solved Solved
0 9 839
2 ACCEPTED SOLUTIONS

sarthak
Participant V

This is a service callout example :

This creates a Jira issue.

<ServiceCallout async="false" continueOnError="false" enabled="true" name="Create-Jira-Issue">
    <DisplayName>Create Jira Issue</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <Set>
            <Headers>
                <Header name="Authorization">Basic ABCD==</Header>
            </Headers>
            <Payload contentType="application/json" variablePrefix="%" variableSuffix="#">
                
                {
    "fields": {
       "project":
       { 
          "key": "SEDEMO"
       },
       "summary": "REST ye merry gentlemen.",
       "description": "Creating of an issue using project keys and issue type names using the REST API",
       "issuetype": {
          "name": "Bug"
       }
   }
}
            </Payload>
            <Verb>POST</Verb>
        </Set>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://apigeesc.atlassian.net/rest/api/2/issue/</URL>
    </HTTPTargetConnection>
</ServiceCallout>

View solution in original post

Not applicable

@apigee1234567 thats right you do have to use the variablePrefix and suffix.

View solution in original post

9 REPLIES 9

sarthak
Participant V

This is a service callout example :

This creates a Jira issue.

<ServiceCallout async="false" continueOnError="false" enabled="true" name="Create-Jira-Issue">
    <DisplayName>Create Jira Issue</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <Set>
            <Headers>
                <Header name="Authorization">Basic ABCD==</Header>
            </Headers>
            <Payload contentType="application/json" variablePrefix="%" variableSuffix="#">
                
                {
    "fields": {
       "project":
       { 
          "key": "SEDEMO"
       },
       "summary": "REST ye merry gentlemen.",
       "description": "Creating of an issue using project keys and issue type names using the REST API",
       "issuetype": {
          "name": "Bug"
       }
   }
}
            </Payload>
            <Verb>POST</Verb>
        </Set>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://apigeesc.atlassian.net/rest/api/2/issue/</URL>
    </HTTPTargetConnection>
</ServiceCallout>

What is the significance of variablePrefix and variableSuffix in the Payload field? I had tried the same thing but without these fields and it did not work out as expected.

Not applicable

@apigee1234567 the variablePrefix and variableSuffix allow you indicate the payload for e.g

<Payload>
{
	"name-1":#namevalue_1%,
	"name-2":#namevalue_2%
}
</Payload>

Where name value_1 and _2 are determined in a previous step.

Can you explain what exactly didn't work by sharing your code

My ServiceCallout policy is as follows: When I do not add variablePrefix and variableSuffix it gives me the error 'Unresolved variable: username'

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 

<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">     
	<DisplayName>Service Callout-1</DisplayName>     
	<Properties/>     
	<Request clearPayload="true" variable="myRequest">         
		<Set> 
			<Headers> 
				<Header name="Content-Type">application/json</Header> 

			</Headers>
			<Verb>POST</Verb>
			<Payload contentType="application/json">
                {"username":"DSX", "serial":"123456", "keyId":"XYZW", "otp":"49d566f76758a88d0367f312fa33419efdf53a81a0f9c5bd8bd578362f87123da49c3ff55dcdd17b33c7a8a9e70b8c1a881c6ea7961d817cbaa1f7414ea3adc9"}
			</Payload>
        	</Set>
	        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
	</Request>
	<Response>calloutResponse</Response>
	<HTTPTargetConnection>
	    <Properties/>
	    <URL>https://auth-beta.factset.com/fetchotpv1</URL>
	</HTTPTargetConnection>
</ServiceCallout>

Not applicable

@apigee1234567 when you don't put the variableprefix and variablesuffix it assumes that anything after "{" is a "username" is a variable and hence the error.

Not applicable

Trace the call and see if the variable is available with a different name, perhaps a typo or you are using the prefix in naming the variable but not in retrieving the variable. If you notice that username is blank in the execution step for your ServiceCallout then indeed that variable is not defined.

Not applicable

@apigee1234567 let me know if you go what you want.

@rajeev@apigee.com : I do have to use variablePrefix and variableSuffix. Without using it results in Undefined Variable

Not applicable

@apigee1234567 thats right you do have to use the variablePrefix and suffix.