python3 call to Apigee returning error "Failed to execute the ExtractVariables"

cboyd
New Member

Hi All,

I am using python 3 to make an API call to Apigee.

It is giving me the error: Failed to execute the ExtractVariables. In the trace I see "Expecting { or [ at line 1" on the Extract variable.

My python code, basically, looks like this:

url2 = "https://myurl.apigee.net/x/fetchrec" 
payload2 = {"Id":"84786875", "token": tokenJWT} 
headers2 = {
  'Content-Type': 'application/json',
  'x-api-key': 'SomeSecretKeyValue',
  'Authorization': 'Basic bAnotherSecret'
} 
response2 = requests.post(url2, headers=headers2, data=payload2)

This is my code in the ExtractVariable:

<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Token">
  <DisplayName>Extract Token</DisplayName>     
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <VariablePrefix>JWT</VariablePrefix>     
  <JSONPayload>         
    <Variable name="token">             
      <JSONPath>$.token</JSONPath>         
    </Variable>     
  </JSONPayload> 
</ExtractVariables>

Using Postman or even a plain curl statement this works fine.

But with python3 it doesn't.

Anyone have any ideas where I am going wrong?

Thanks.

0 2 318
2 REPLIES 2

Your extractVariables policy looks fine. If it works for some clients, and not others, the problem is probably on the client side.

I don't know python3 or the requests module, but I think it is probably the data=payload2 thing.

In short your data over the wire is not being serialized as json, as you intend. (And as Postman does for you). You should be able to see this yourself by using the Trace UI in Apigee. This video covers that.

This example on stackoverflow suggests using the json= key:

import requests
data = {'sender':   'Alice',
    'receiver': 'Bob',
    'message':  'We did it!'}
r = requests.post("http://localhost:8080", json={'json_payload': data}) 

for your code, you'd probably want something like this:

response2 = requests.post(url2, headers=headers2, json=payload2)

Not applicable

you can try like this

 response = requests.post(url, data=json.dumps(payload), headers=headers)