Apex Callout报HTTP Version not supported(505)错误,求SFDC删Amazon文件解决方法
Hey there, let's work through this 505 error you're hitting when trying to delete Amazon platform files from Salesforce. That error almost always means your request is using an HTTP version that Amazon's servers don't support—so let's break down the fixes step by step.
Common Causes & Fixes
1. Explicitly Set the HTTP Version to 1.1
By default, Salesforce's HttpRequest class might use HTTP/1.0, which many modern APIs (including Amazon's) no longer support. The quick fix here is to explicitly set the protocol to HTTP/1.1 when building your request:
HttpRequest req = new HttpRequest(); // ... other setup like endpoint, method, headers ... req.setProtocol('HTTP/1.1'); // Add this critical line Http http = new Http(); HttpResponse res = http.send(req);
This is the most likely fix for your 505 error—Amazon's APIs require HTTP/1.1 or higher, so forcing this version should resolve the version mismatch.
2. Verify Your Date Format for Signing
While the 505 is a version issue, invalid date formatting in your signature can sometimes trigger unexpected server responses. Double-check these details in your date generation code:
- You're mixing
formatGmtandformatGMT(note the uppercase MT in the second call). Stick toformatGMTconsistently to avoid formatting inconsistencies. - Your final
dateStringfollows ISO 8601 format (yyyy-MM-dd'T'HH:mm:ss.SSS'Z'), which is correct for Amazon's signature requirements, but confirm the generated string doesn't have typos (like extra characters or missing time components).
3. Check for Conflicting Request Headers
Some headers can interfere with HTTP version negotiation. For example:
- Avoid setting
Connection: close(this is more common in HTTP/1.0) - Instead, explicitly set
Connection: keep-aliveto align with HTTP/1.1 standards:req.setHeader('Connection', 'keep-alive');
4. Test with a Simplified Request
To isolate the issue, create a minimal test request without all the signing logic. This will help confirm if the HTTP version is the root cause:
HttpRequest testReq = new HttpRequest(); testReq.setEndpoint('https://your-amazon-api-endpoint'); // Use your actual endpoint testReq.setMethod('DELETE'); testReq.setProtocol('HTTP/1.1'); Http testHttp = new Http(); HttpResponse testRes = testHttp.send(testReq); System.debug('Test Response Status: ' + testRes.getStatus()); System.debug('Test Response Status Code: ' + testRes.getStatusCode());
If this test request returns a valid status (not 505), then the problem was indeed the missing HTTP version setting in your original code.
Final Notes
Once you add the setProtocol('HTTP/1.1') line to your original request, re-test the full signed request. If you still hit issues, double-check your signature generation logic (since invalid signatures can sometimes masquerade as other errors), but the 505 points directly to an HTTP version mismatch.
内容的提问来源于stack exchange,提问作者T.Visp

