配置代理后Karate调用部分开源API仍返回403错误求助
企业代理环境下Karate调用部分API返回403错误排查
环境信息
- Java版本:1.8
- Maven版本:3.6.3
API调用状态
无法正常调用的API
- https://gorest.co.in/public/v2/users
- https://reqres.in/api/users/2
可正常调用的API
- http://www.7timer.info/bin/astro.php?lon=113.2&lat=23.1&ac=0&unit=metric&output=json&tzshift=0
- https://dummy.restapiexample.com/api/v1/employee/1
- https://fakerestapi.azurewebsites.net/api/v1/Activities
- https://randomuser.me
- https://httpbin.org/#/HTTP_Methods/get_get
- https://makeup-api.herokuapp.com/api/v1/products.json?brand=maybelline
- http://numbersapi.com/random/math
- http://jsonplaceholder.typicode.com/posts/1
Feature文件内容
Feature: Sample API Test Scenario: get user details with path parameter * configure proxy = 'http:myproxy:8080' * configure ssl = true; Given header Content-Type = 'application/json' And header Accept = 'application/json' When url 'https://reqres.in/api/users/2' When method GET Then status 200 * print 'result: ',response
POM依赖
<!-- https://mvnrepository.com/artifact/com.intuit.karate/karate-junit4 --> <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit4</artifactId> <version>1.3.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting --> <dependency> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>5.7.4</version> </dependency>
错误信息
status code was: 403, expected: 200, response time in milliseconds: 464, url: https://reqres.in/api/users/2, response: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Could not connect to server</title> <link rel="stylesheet" href="https://reqres.in/%24%24%24%26%3f%26%3f%24%24%24?cmd=get_file&arg=block_style.css&sid=90709ACBF52361FBE728AFA2A8AE02A868DB9E86" type="text/css"> </head> <body> <div id="pagecontainer"> <img class="icon" src="https://reqres.in/%24%24%24%26%3f%26%3f%24%24%24?cmd=get_file&arg=images/block.png&sid=7B320C491CF4319D6D2C8C17BA9FE6DA2ECA16B8"> <!--Icon for block type--> <h1>Could not connect to server</h1> <div class="row"> <p class="label">Overview:</p> <p class="item">Could not connect to reqres.in </p> </div> <div class="row"> <p class="label">Details:</p> <p class="item">Peer suddenly disconnected</p> </div> <div id="options"> <p class="label">Options:</p> <form action=""><input type="button" class="button" onclick="history.back();" value=" Go Back "></form><p class="item">Click to return to the previous page:</p> <p class="last-item">Make sure the URL is correct, then try to reload the page.</p> </div> <div><img class="logo" title="Forcepoint" src="https://reqres.in/%24%24%24%26%3f%26%3f%24%24%24?cmd=get_file&arg=images/wslogo_block_page.png&sid=46D63EFDD3551F01875504354D49CF431151A9BE" alt="Forcepoint Logo"> <div style="clear: both; overflow: hidden; height:1px;"></div> </div> </div> </body> </html>
排查建议
修正代理配置格式:当前代理配置
'http:myproxy:8080'缺少//,正确格式应为'http://myproxy:8080',格式错误会导致HTTPS请求代理解析异常,是部分API失败的常见原因。添加代理认证(若需要):如果企业代理要求用户名密码验证,需更新代理配置:
* configure proxy = { uri: 'http://myproxy:8080', username: 'your-proxy-user', password: 'your-proxy-pass' }
- 调整SSL信任设置:部分HTTPS API可能因代理证书未被信任导致失败,可临时设置信任所有证书测试:
* configure ssl = { trustAll: true }
注意:生产环境不建议使用此配置,需导入企业代理的根证书到JVM信任库。
- 简化请求头:GET请求无需设置
Content-Type头,移除该头后重试,避免部分API对请求头的严格校验:
Given header Accept = 'application/json'
- 检查企业安全网关拦截:错误信息中出现Forcepoint标识,说明请求被企业安全网关拦截,需联系IT部门将目标API加入白名单。
内容的提问来源于stack exchange,提问作者Vijay Gupta
相关产品推荐
相关产品推荐

