简体   繁体   English

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request binance

[英]org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request binance

I'm trying to post order to the binance by using RestTemplate.我正在尝试使用 RestTemplate 向币安发布订单。 The next simplified code:下一个简化代码:

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add("user-agent",
            "Mozilla/5.0 " +
                    "(Windows NT 10.0; Win64; x64) " +
                    "AppleWebKit/537.36 (KHTML, like Gecko) " +
                    "Chrome/54.0.2840.99 " +
                    "Safari/537.36");
    headers.add(X_MBX_APIKEY, apiKey);

    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

    long time = new Date().getTime();
    String params1 = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.005&recvWindow=50000" +
            "&timestamp=" + time;
    String signature = encodeSHA256(secretKey, params1);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://api.binance.com/api/v3/order/test")
            .queryParam("symbol", "LTCBTC")
            .queryParam("side", "BUY")
            .queryParam("type", "LIMIT")
            .queryParam("timeInForce", "GTC")
            .queryParam("quantity", "1")
            .queryParam("price", "0.005")
            .queryParam("recvWindow", "50000")
            .queryParam("timestamp", time)
            .queryParam("signature", signature);
    ResponseEntity<String> response = restTemplate.exchange(builder.build().toUri(), HttpMethod.POST, entity, String.class);

So I get next exception:所以我得到下一个例外:

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{"code":-1104,"msg":"Not all sent parameters were read; read '9' parameter(s) but was sent '10'."}]
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:170)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:714)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:602)

The same request in postman works well.邮递员中的相同请求效果很好。 So what is the reason of this error?那么这个错误的原因是什么呢? Because the quantity of parameters is 9 in request.因为请求中的参数数量是9个。 Maybe binance api recognize the apiKey in header as 10-th parameter?也许 binance api 将标头中的 apiKey 识别为第 10 个参数? Thank you in advance.先感谢您。

I don't no why restTemplate is not working, but I copied the code from postman that is using okhttp3.我不知道为什么 restTemplate 不起作用,但我从使用 okhttp3 的邮递员那里复制了代码。 And now it works.现在它起作用了。

        long time = new Date().getTime();
        String params = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1" +
        "&price=0.004&recvWindow=50000" +
                "&timestamp=" + time;
        String signature = encodeSHA256(marketKey, params);
        Request request = new Request.Builder()
                .url("https://api.binance.com/api/v3/order" +
                        "?symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1" +
                        "&price=0.004&recvWindow=50000" +
                        "&timestamp=" + time +
                        "&signature=" + signature)
                .post(new RequestBody() {
                    @Nullable
                    @Override
                    public MediaType contentType() {
                        return null;
                    }

                    @Override
                    public void writeTo(BufferedSink bufferedSink) throws IOException {

                    }
                })
                .addHeader("X-MBX-APIKEY", apiKey)
                .build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.web.client.HttpClientErrorException$BadRequest: 400 错误请求 - org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request 我收到错误 org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request - I am getting the Error org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request 我收到错误消息“ org.springframework.web.client.HttpClientErrorException $ BadRequest:400错误请求” - I'm getting error “org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request” org.springframework.web.client.HttpClientErrorException:400错误的请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request org.springframework.web.client.HttpClientErrorException: 400 错误的 PUT 请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request for PUT org.springframework.web.client.HttpClientErrorException:RestTemplate 中的 400 错误请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request in RestTemplate RestTemplate.postForObject - 错误:org.springframework.web.client.HttpClientErrorException:400错误请求 - RestTemplate.postForObject - Error: org.springframework.web.client.HttpClientErrorException: 400 Bad Request org.springframework.web.client.HttpClientErrorException:400使用HttpEntity restTemplate.exchange的错误请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request using HttpEntity restTemplate.exchange org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity - org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity org.springframework.web.client.HttpClientErrorException:400空 - org.springframework.web.client.HttpClientErrorException: 400 null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM