【问题标题】:Get command fails with 400 but works in Postman [duplicate]获取命令以 400 失败,但在邮递员中工作 [重复]
【发布时间】:2021-02-04 23:17:15
【问题描述】:

我有一个 400 失败的空手道 get 测试,但如果我将所有内容复制到邮递员,它就可以工作。此外,如果我将 get 更改为代码中的帖子并进行测试并在测试中添加一个空的请求正文,它就可以工作。我做错了什么?

这里是要测试的端点的代码:

@Log
@Named
@Profile
@RestController
@Singleton
@Path("/commercial/deposits/accounts")
@Api(value = "/commercial/deposits/accounts")
@Produces({"application/vnd.com.capitalone.api+v3+json", "application/vnd.com.capitalone.api+v3+xml",
           "application/vnd.com.capitalone.api+v3+javascript", "application/javascript", MediaType.APPLICATION_JSON,
           MediaType.APPLICATION_XML})
@Consumes({"application/vnd.com.capitalone.api+v3+json", "application/vnd.com.capitalone.api+v3+xml",
            MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class CommercialAccountsRSResource  {

    @Inject
    private CommercialAccountsRSService commercialAccountsRSService;

    /**
     * Account Summary - retrieves current bank account balances for accounts specified
     *
     * @param entityRequest Standard {@link EntityRequest} fields
     *
     * @return A BankThing
     */

    @GET
    @JSONP(queryParam = JSONP.DEFAULT_QUERY)
    @ApiOperation(value = "Get account balances for accountIds specified.", notes = "Get account balances for accountIds specified.", response = AccountSummaryResponse.class)
    @ApiResponses({
            @ApiResponse(code = HttpURLConnection.HTTP_OK, message = ChassisHTTPStatusMessageConstants.SUCCESS_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_NOT_AUTHORITATIVE, message = ChassisHTTPStatusMessageConstants.PARTIAL_SUCCESS_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = ChassisHTTPStatusMessageConstants.BAD_REQUEST_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = ChassisHTTPStatusMessageConstants.UNAUTHORIZED_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = ChassisHTTPStatusMessageConstants.FORBIDDEN_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = ChassisHTTPStatusMessageConstants.NOT_FOUND_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_BAD_METHOD, message = ChassisHTTPStatusMessageConstants.METHOD_NOT_ALLOWED_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = ChassisHTTPStatusMessageConstants.NOT_ACCEPTABLE_MESSAGE),
            @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = ChassisHTTPStatusMessageConstants.INTERNAL_SERVER_ERROR_MESSAGE)})
    public AccountSummaryResponse accountSummaryResponse(@BeanParam EntityRequest entityRequest,
                                                         @ApiParam(value = "Customer Account number", name = "accountReferenceId",  required = true, allowMultiple = true) @QueryParam("accountReferenceId") String accountReferenceId,
                                                         @ApiParam(value = "Bank number", name = "bankNumber",  required = true, allowMultiple = true) @QueryParam("bankNumber") String bankNumber)
    {
        CommercialAccountsRSResourceUtil.validateInput(accountReferenceId, bankNumber);
        return commercialAccountsRSService.accountSummaryResponse(accountReferenceId, bankNumber);
    }

这是空手道场景:

Feature: Test DDA Account Summary

  Background:
    * url baseURL
    And headers { Api-Key: 'CMIX', Accept: 'application/json;v=3', Content-Type: 'application/json;v-3',Authorization: '#(bearerToken)' }

  @local
  Scenario: Get single account successfully
    Given path '/dda-account-summary-web/commercial/deposits/accounts'
    And param accountReferenceId = '0000030651'
    And param bankNumber = '0030'
    When method GET
    Then status 200
    And match response == read('AccountSummarySingleSuccess.json')

结果如下:

08:36:16.316 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - << lock released, cached callSingle: classpath:oAuthToken.feature
08:36:16.324 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - baseURL: http://localhost:11400 
08:36:16.338 [ForkJoinPool-1-worker-1] DEBUG com.intuit.karate - request:
1 > GET http://localhost:11400/dda-account-summary-web/commercial/deposits/accounts?accountReferenceId=0000030651&bankNumber=0030
1 > Accept: application/json;v=3
1 > Accept-Encoding: gzip,deflate
1 > Api-Key: CMIX
1 > Authorization: Bearer xxx
1 > Connection: Keep-Alive
1 > Content-Type: application/json;v-3
1 > Host: localhost:11400
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_271)

08:36:16.390 [ForkJoinPool-1-worker-1] DEBUG com.intuit.karate - response time in milliseconds: 50.62
1 < 400
1 < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
1 < Connection: close
1 < Content-Type: application/json; v=3
1 < Date: Thu, 04 Feb 2021 13:36:16 GMT
1 < Expires: 0
1 < Pragma: no-cache
1 < Set-Cookie: JSESSIONID=09DC5825407678630098BCD5087425DA; Path=/dda-account-summary-web; HttpOnly
1 < Transfer-Encoding: chunked
1 < X-Content-Type-Options: nosniff
1 < X-Frame-Options: DENY
1 < X-XSS-Protection: 1; mode=block
{
  "timestamp": "2021-02-04T13:36:16.385+00:00",
  "status": 400,
  "error": "Bad Request",
  "message": "",
  "path": "/dda-account-summary-web/commercial/deposits/accounts"
}

【问题讨论】:

  • 因为我不确定哪里出了问题,所以我不确定什么是不必要的,但这里有一个 curl 命令有效: curl --location --request GET 'localhost:11400/dda-account-summary-web/commercial/deposits/…' \ --header '接受: application/json;v=3' \ --header 'Api-Key: CMIX' \ --header 'Content-Type: application/json;v=3' \ --header '授权: Bearer xxx' \ --header 'Cookie: JSESSIONID=4F26ECB17337846AA060C4A1162B8A5E' \ --data-raw ''
  • 服务器端的 java 端对我个人来说没用。 cURL 也没有帮助,因为我无法尝试请求。无论如何尝试回答,如果这没有帮助,我会通过

标签: karate


【解决方案1】:

您的命令似乎非常规,将主体作为 GET 的一部分。

请尝试空手道 1.0 系列:https://github.com/intuit/karate/wiki/1.0-upgrade-guide

并在method 步骤之前添加这一行:

And request ''

如果这不起作用,则可能是一个错误,但除非您提供一种复制方式,否则我无能为力:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

编辑:不幸的是,邮递员会自动添加一个 Accept 标头,所以也试试这个:

And header Accept = 'application/json'

【讨论】:

  • GET 没有正文。我只添加了一个正文来查看 POST 是否可以正常工作,但最终我需要 GET。我将尝试构建一个骨架并将其作为问题提交。
  • 其实我发现了错误。对于 ContentType,我有 'application/json;v-3'。一旦我将其更改为 v=3,它就开始工作了。之前看的没看到。无论如何感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2021-12-23
  • 2016-02-18
  • 2021-11-15
  • 2020-08-25
  • 2020-08-07
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多