Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

how to send data request and response of the query to pubsub via servicecallout

"I'm attempting to log API request and response data to a Google Cloud Pub/Sub topic within an Apigee proxy's PostFlow using a Service Callout. I'm encountering 400 errors from Pub/Sub and suspect the request formatting is incorrect.

Specifically, I'm trying to capture the full request (including headers and payload) and the complete response (headers and payload) and send this as a structured log entry to Pub/Sub.

Could you provide guidance on the correct JSON structure and encoding (e.g., Base64) required by Pub/Sub for this type of logging? Are there any built-in Apigee policies or efficient JavaScript techniques to achieve this without significantly impacting proxy performance? I'm particularly concerned about the overhead of processing the request and response data within Apigee before sending it to Pub/Sub.

Any best practices or example code snippets would be greatly appreciated."

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout continueOnError="false" enabled="true" name="SC-LogToPubSub">
<DisplayName>SC-LogToPubSub</DisplayName>
<Request variable="myrequestvariable">
<Set>
<Verb>POST</Verb>
<Payload contentType="application/json">
{ request.content }
</Payload>
</Set>
</Request>
<Response>pubsublogs</Response>
<HTTPTargetConnection>
<Authentication>
<GoogleAccessToken>
<Scopes>
<Scope>https://www.googleapis.com/auth/pubsub</Scope>
</Scopes>
</GoogleAccessToken>
</Authentication>
<SSLInfo>
<Enabled>true</Enabled>
<IgnoreValidationErrors>false</IgnoreValidationErrors>
</SSLInfo>
<Properties>
<Property name="success.codes">2xx, 3xx, 4xx</Property>
</Properties>
<URL>https://pubsub.googleapis.com/v1/projects/prima

0 2 76
2 REPLIES 2

Hi pato17,

To know more about the required payload format, please review the PubSub REST API docs here

Here's a Service Callout that you could use to send base64-encoded messages that adhere to the PubSub API spec:

<ServiceCallout continueOnError="true" enabled="true" name="SC-LogToPubSup">
  <Request>
    <Set>
      <Verb>POST</Verb>
      <Payload contentType="application/json">
{
  "messages": [
    {
        "data": "{encodeBase64(message.content)}"
    }
  ]
}
</Payload>
    </Set>
  </Request>
  <Response>calloutResponse</Response>
  <HTTPTargetConnection>
    <Properties/>
    <URL>https://pubsub.googleapis.com/v1/projects/your-project-id-here/topics/demo:publish</URL>
    <Authentication>
      <GoogleAccessToken>
        <Scopes>
          <Scope>https://www.googleapis.com/auth/pubsub</Scope>
        </Scopes>
      </GoogleAccessToken>
    </Authentication>
  </HTTPTargetConnection>
</ServiceCallout>

 

exactly what i was looking for thx