如何在SAP CPI中通过简易HTTP适配器实现先GET取Token再POST JSON到S4HANA
Alright, let's walk through how to pull this off in SAP CPI using the Simple HTTP Adapter—getting that auth token via GET, then reusing it for your JSON POST to S/4HANA. Here's the step-by-step breakdown:
Step 1: Set up the GET Request to Fetch Your Auth Token
First, add a Simple HTTP Receiver Adapter to your iFlow to call the token endpoint:
- In the Connection tab:
- Plug in your token service URL (e.g.,
https://your-s4hana-domain.com/oauth2/token) - Set the HTTP method to
GET - Configure authentication matching what your token service requires (most often Basic Auth, so enter the client ID and secret here)
- Plug in your token service URL (e.g.,
- In the Processing tab:
- Ensure the response handling is set to accept JSON (since token responses are almost always JSON-formatted)
Step 2: Extract and Store the Token in CPI Exchange Properties
Right after the GET request, add a Content Modifier to pull the token out of the response and save it for later use:
- Switch to the Message Properties tab:
- Create a new property (name it something like
authTokenfor clarity) - For the value, use JSONPath to target the token field in the response. If your token comes back as
{"access_token": "your-token-here"}, use$.access_token - Set the scope to Exchange—this makes the token accessible across all steps in your iFlow
- Create a new property (name it something like
Step 3: Configure the POST Request to S/4HANA with the Token
Now add a second Simple HTTP Receiver Adapter for your S/4HANA service call:
- In the Connection tab:
- Enter your S/4HANA service endpoint (e.g.,
https://your-s4hana-domain.com/sap/opu/odata/sap/API_MATERIAL_SRV/A_Material) - Set the HTTP method to
POST - Skip the adapter-level authentication here—we'll use the token we fetched instead
- Enter your S/4HANA service endpoint (e.g.,
- In the Processing tab:
- Set the Request Body format to
JSON(this ensures your payload is sent as JSON instead of XML) - Head to the Headers section:
- Add a new header named
Authorization - Set its value to
Bearer ${property.authToken}—this injects the stored token into the header using CPI's property placeholder syntax
- Add a new header named
- Set the Request Body format to
Pro Tips to Avoid Headaches
- Test your token GET request first in Postman or curl to confirm it returns a valid JSON response with the expected token field (usually
access_token). This saves you troubleshooting time in CPI later. - If your token has an expiration, add a Cache component to store the token temporarily. This avoids hitting the token service on every iFlow run, which is more efficient and avoids rate limits.
- Double-check that your POST JSON payload matches the schema expected by your S/4HANA service. You can either pass the incoming JSON directly (if it's already correct) or use a Message Mapping to convert it if needed.
- Use the CPI Message Monitor to debug: check the message logs to verify the token was extracted correctly and that the
Authorizationheader is present in the POST request.
内容的提问来源于stack exchange,提问作者Codrin Strîmbei
相关产品推荐
相关产品推荐

