Integration

How to Follow an HTTP Redirect in SAP CPI

Vlado Balko

The HTTP adapter does not follow redirects. Here is how to handle a 302 — and a trap that gives you a 403 on pre-signed URLs.

Many APIs answer a request with 302 Moved Temporarily and a Location header, instead of returning the data directly. The SAP CPI HTTP adapter does not follow that redirect automatically. This post shows how to handle it, and one trap that costs a lot of time when the redirect points to an AWS pre-signed URL.

TL;DR — CPI does not follow redirects. Read the Location header and call it again. But if that URL is a pre-signed URL (AWS S3/CloudFront, Azure SAS, ...), the HTTP adapter re-encodes it and you get a 403. The safe fix is to make that second call from a Groovy script with java.net.URL , which sends the URL exactly as it is. Step 1: handle the redirect

The CPI HTTP adapter has no “follow redirects” option. By default it even throws an exception on a 3xx response. The standard way to handle it:

  1. On the first HTTP call, set Throw Exception on Failure = OFF.

  2. Copy the Location response header into a property (for example

TargetLocation ).

  1. Make a second HTTP call to that URL.

For most APIs this is enough. The problems start when the Location is a pre-signed URL.

The trap: a 403 on the second call

Here is a real example. The Basware API (public API behaviour, nothing customer-specific) returns the invoice attachment as a redirect:

GET /v1/accountingDocuments/{id}/attachments/{attachmentId}

-> 302, Location: https://api.us.basware.com/cfsecure/.../INVOICE%20%28copy%202 ?Expires=...&Signature=W-4m2LqUesX~Bxpzq6CfZ~Upf87...&Key-Pair-Id=...

The second call to that pre-signed URL fails with 403 Forbidden. But the same URL works in Postman or Bruno with “follow redirects” on. Why?

Turn on the trace and compare the Location you received with the URL CPI really sent ( RealDestinationUrl ). They are different:

Path: received ...INVOICE%20%28copy%202%29%202026.pdf

CPI sent ...INVOICE%20(copy%202)%202026.pdf (%28/%29 decoded to

Query: received ...W-4m2LqUesX~Bxpzq6CfZ~Upf87...

CPI sent ...W-4m2LqUesX%7EBxpzq6CfZ%7EUpf87... (every

"~" encoded to %7E)

A pre-signed URL is signed over the exact bytes of the URL. If one character changes its encoding, the signature no longer matches, and you get a 403. Postman sends the URL exactly as it is. CPI changes it.

Note: this is not an AWS problem. It happens with any URL where the exact encoding must stay the same — AWS S3/CloudFront, Azure SAS tokens, Google Cloud signed URLs, and others.

Why CPI changes the URL

The answer is not in the SAP Help Portal — it is in the Apache Camel source code, which the HTTP adapter is built on. Two layers change the URL:

Camel UnsafeUriCharactersEncoder . Camel keeps a list ("BitSet") of characters it treats as unsafe and encodes them. The tilde is on that list ( unsafeCharactersHttp.set('~') in the source), so every ~ becomes %7E . Apache HttpClient. It normalizes the URI following RFC 3986. Parentheses are allowed in a path, so HttpClient decodes %28 / %29 back to ( ) .

Neither layer can be turned off in the adapter. If you want to check it yourself: UnsafeUriCharactersEncoder :

https://github.com/apache/camel/blob/main/core/camel- util/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java HttpProducer : https://github.com/apache/camel/blob/main/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java

Reading the Camel source is a good habit — it answers questions the docs leave open.

What does not fix it

CamelHttpUri / CamelHttpQuery headers. They only change which URL goes into the adapter. The adapter still runs the same encoder. (I confirmed this with a trace: still 403.)

Adapter settings. There is no “raw URL” or “disable encoding” option.

The %25 escaping trick (SAP KBA 3131448, which I also described in an earlier post, "SAP Cloud Integration — URL escaping hack"). It helps with characters the adapter decodes (the parentheses): send %2528 , and it stays %28 . But it cannot help with the tilde, because the adapter always encodes ~ to %7E and there is no input that keeps a plain ~ . So a configuration-only fix is not possible here. The fix: call the URL from Groovy

Since no setting stops the encoding, make the second call outside the adapter — from a Groovy script with java.net.URL . new URL(str) sends the URL exactly as it is, like a browser or Postman.

Replace the second HTTP call (the Request-Reply step and its receiver channel) with one Groovy script step that reads the pre-signed URL from the property and downloads the file:

import com.sap.gateway.ip.core.customdev.util.Message
import java.net.HttpURLConnection
import java.net.URL

/**

* Downloads a file from a pre-signed URL captured from the 302 "Location" head * (stored in property TargetLocation).

*

* Why Groovy and not the HTTP adapter:

* The CPI HTTP adapter (Apache Camel + HttpClient) always re-encodes the URL - * turns '~' into '%7E' and decodes '%28'/'%29' to '(' ')'. A pre-signed URL is * signed over the exact bytes, so any change -> 403. java.net.URL sends the UR * exactly as it is.

*/

def Message processData(Message message) {

def messageLog = messageLogFactory.getMessageLog(message)

def rawUrl = message.getProperty("TargetLocation")?.toString()

// Guard: nothing to download
if (rawUrl == null || rawUrl.trim().isEmpty()) {
if (messageLog != null) {

messageLog.addCustomHeaderProperty("Termination_Reason", "Missing_T

}

throw new IllegalStateException("TargetLocation is empty - no URL to do

}

HttpURLConnection conn = null

try {

URL url = new URL(rawUrl) // keeps the URL byte-for- conn = (HttpURLConnection) url.openConnection()

conn.setRequestMethod("GET")

conn.setInstanceFollowRedirects(true)

conn.setConnectTimeout(60000)

conn.setReadTimeout(60000)

// Pre-signed URL is self-authenticating -> no Authorization header.

int rc = conn.getResponseCode()

if (messageLog != null) {

messageLog.addCustomHeaderProperty("Attachment_HTTP_Status", String

}
if (rc >= 200 && rc < 300) {
// Read the body in 8 KB chunks. Fine for normal documents;
// for very large files, use a streaming pattern to avoid OOM.

def buffer = new ByteArrayOutputStream()

conn.getInputStream().withStream { input -> byte[] chunk = new byte[8192] int n

while ((n = input.read(chunk)) != -1) {

buffer.write(chunk, 0, n)

}
}

byte[] data = buffer.toByteArray()

message.setBody(data)

def ct = conn.getContentType()

if (ct != null) { message.setHeader("Content-Type", ct) }

message.setHeader("CamelHttpResponseCode", rc)

if (messageLog != null) {

messageLog.addCustomHeaderProperty("Attachment_Size_Bytes", Str

}
} else {

def err = ""

try {

def es = conn.getErrorStream()

if (es != null) { es.withStream { err = it.getText("UTF-8") } }
} catch (ignored) { }
if (messageLog != null) {

messageLog.addCustomHeaderProperty("Termination_Reason", "Downl messageLog.addCustomHeaderProperty("Error_Detail", "HTTP " + rc

}

throw new RuntimeException("Download failed, HTTP " + rc + ": " + e

}
} finally {
if (conn != null) { conn.disconnect() }
}
return message
}

A note on using Groovy for HTTP

SAP recommends the HTTP adapter by default, and that is a good rule. But for a public pre-signed URL, the usual reasons against Groovy do not apply: no Cloud Connector, no client certificate, no proxy needed on Cloud Foundry. Two things still matter, and the script handles both: add MPL log entries so the call is visible in monitoring, and be careful with memory for very large files (stream instead of loading the full body).

Summary

The CPI HTTP adapter does not follow redirects — read the Location header and call again.

A 403 on a pre-signed URL is usually URL re-encoding, not authentication, and it is not AWS-specific.

The adapter re-encodes the URL in two layers ( ~ → %7E and %28 → ( ), and this cannot be turned off. When unsure, read the Camel source.

The %25 trick (KBA 3131448) fixes the parentheses but not the tilde.

To send the URL exactly as it is, use a Groovy script with java.net , and add MPL logs.

References

SAP KBA 3131448 — HTTP Receiver adapter discards encoding of special characters in address

SAP Cloud Integration — URL escaping hack (my earlier post):

34a45f5a1f37

Apache Camel UnsafeUriCharactersEncoder :

https://github.com/apache/camel/blob/main/core/camel- util/src/main/java/org/apache/camel/util/UnsafeUriCharactersEncoder.java Apache Camel HttpProducer :

SMARTER TECH, SMARTER BUSINESS

Cloud, AI a parťáci. Integrace, vývoj, automatizace a AI na SAP Business Technology Platform a Microsoft.

Napište nám →

© 2026 CLAIMATE · CLOUD · AI · MATES

SAP PartnerEdge · Microsoft Partner

SMARTER TECH, SMARTER BUSINESS

Cloud, AI a parťáci. Integrace, vývoj, automatizace a AI na SAP Business Technology Platform a Microsoft.

Napište nám →

© 2026 CLAIMATE · CLOUD · AI · MATES

SAP PartnerEdge · Microsoft Partner

SMARTER TECH, SMARTER BUSINESS

Cloud, AI a parťáci. Integrace, vývoj, automatizace a AI na SAP Business Technology Platform a Microsoft.

Napište nám →

© 2026 CLAIMATE · CLOUD · AI · MATES

SAP PartnerEdge · Microsoft Partner

Integration

How to Follow an HTTP Redirect in SAP CPI