binance / binance-connector-dotnet

Lightweight connector for integration with Binance API
MIT License
207 stars 69 forks source link

Problem with PostAsync request in "TRADE" Section from Postman collection #33

Closed MGabala closed 1 year ago

MGabala commented 1 year ago

Hello, i am working with binanceAPI and i get stuck at this point: {"code":-1102,"msg":"Mandatory parameter 'symbol' was not sent, was empty/null, or malformed."}

This is my code:

    public CRUD_TESTNET()
    {
        _httpClient.BaseAddress = new Uri("https://testnet.binance.vision");
        _httpClient.Timeout = new TimeSpan(0, 0, 5);
        _httpClient.DefaultRequestHeaders.Clear();
        _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _httpClient.DefaultRequestHeaders.Add("X-MBX-APIKEY", Environment.GetEnvironmentVariable("APIKEY"));
        _httpClient.DefaultRequestHeaders.Add("SecretKey", Environment.GetEnvironmentVariable("SECRETKEY"));

    }
private async Task POSTNewOrder()
        {
            string? signature = Environment.GetEnvironmentVariable("SIGNATURE");
            var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            // EXAMPLE: XRPBUSD
            Console.Write("Choose pair: "); string? pair = Console.ReadLine();
            //EXAMPLE: BUY / SELL
            Console.Write("Side: "); string? side = Console.ReadLine();
            //EXAMPLE: LIMIT / MARKET / STOP_LOSS / STOP_LOSS_LIMIT / TAKE_PROFIT / TAKE_PROFIT_LIMIT / LIMIT_MAKER
            Console.Write("Type: "); string? type = Console.ReadLine();
            //EXAMPLE: GTC (good till canceled) / FOK (fill or kill) / IOC (immediate or cancel)
            Console.Write("TimeInForce: "); string? timeInForce = Console.ReadLine();
            //EXAMPLE: Quantity: 100
            Console.Write("Quantity: "); string? quantity = Console.ReadLine();
            //EXAMPLE: Price: 350
            Console.Write("Price: "); string? price = Console.ReadLine();
            string? query = $"symbol={pair}&side={side}&type={type}&timeInForce={timeInForce}&quantity={quantity}&price={price}&timestamp={timestamp}&signature={signature}";
            var request = new
            {
                symbol = pair,
                side = side,
                type = type,
                timeInForce = timeInForce,
                quantity = quantity,
                price = price,
                timestamp = timestamp,
                signature = signature
            };
            var json = JsonConvert.SerializeObject(request);
            var stringContent = new StringContent(json);
            try
            {

                var response = await _httpClient.PostAsync("/api/v3/order?", stringContent);
                //response.EnsureSuccessStatusCode();
                var content = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"{content.ToString()}");
                Console.WriteLine($"{response.Headers}");
                Console.WriteLine($"{response.StatusCode}");

            }
            catch (Exception exception)
            {
                Console.WriteLine("\nSorry, cannot proceed your request.." + $"\n{exception.Message}");

            }

This is response state:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Connection: keep-alive
  Date: Tue, 08 Nov 2022 16:25:09 GMT
  Server: nginx
  x-mbx-uuid: 0aee5274-30fc-405d-a68a-a2fe57334ef0
  x-mbx-used-weight: 1
  x-mbx-used-weight-1m: 1
  Strict-Transport-Security: max-age=31536000; includeSubdomains
  X-Frame-Options: SAMEORIGIN
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  Content-Security-Policy: default-src 'self'
  X-Content-Security-Policy: default-src 'self'
  X-WebKit-CSP: default-src 'self'
  Cache-Control: no-cache, no-store, must-revalidate
  Pragma: no-cache
  X-Cache: Error from cloudfront
  Via: 1.1 6c38ff4c7648bbb26bea641498fdefb0.cloudfront.net (CloudFront)
  X-Amz-Cf-Pop: VIE50-P1
  X-Amz-Cf-Id: SuFO8l1L5xsnzqZQ3fqRFmXwIyP6l6cRoRBjRnK94wmQRK305Ct47g==
  Content-Type: application/json;charset=UTF-8
  Content-Length: 95
  Expires: 0
}}

I would appreciate if you will help me.

Actually i am building API connector and i found something. All methods from "TRADE" are working in POSTMAN, but not from my C# code. Even GetAsync dont work. This is another one method:

        private async Task GETAllOwnOrders(long timestamp, string signature)
        {

            try
            {
                var response = await _httpClient.GetAsync($"/api/v3/allOrders?symbol=BNBUSDT&timestamp={timestamp}&signature={signature}");
                response.EnsureSuccessStatusCode();
                var content = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"\nCurrent Open Orders {content}");
            }
            catch (Exception exception)
            {
                Console.WriteLine("\nSorry, cannot proceed your request.." + $"\n{exception.Message}");

            }

        }

This is link to the repository: https://github.com/MGabala/BINANCE-BOT

2pd commented 1 year ago

Can you print the error message that returned from server?

MGabala commented 1 year ago

Can you print the error message that returned from server?

I belive you mean this:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Connection: keep-alive
  Date: Wed, 09 Nov 2022 08:04:28 GMT
  Server: nginx
  x-mbx-uuid: 51df79e9-d749-439b-8a92-3337f4ded623
  x-mbx-used-weight: 1
  x-mbx-used-weight-1m: 1
  Strict-Transport-Security: max-age=31536000; includeSubdomains
  X-Frame-Options: SAMEORIGIN
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  Content-Security-Policy: default-src 'self'
  X-Content-Security-Policy: default-src 'self'
  X-WebKit-CSP: default-src 'self'
  Cache-Control: no-cache, no-store, must-revalidate
  Pragma: no-cache
  X-Cache: Error from cloudfront
  Via: 1.1 19d23243200e63f987eb95cd84ad557c.cloudfront.net (CloudFront)
  X-Amz-Cf-Pop: VIE50-P1
  X-Amz-Cf-Id: NK54UqpsuubPWXurGTNI0G57SjEhSYuxnF9NycGlrG1uhb7yoKF6uA==
  Content-Type: application/json;charset=UTF-8
  Content-Length: 95
  Expires: 0
}}

Response status code does not indicate success: 400 (Bad Request).

BTW you can download this code and use it on your own. Its small piece of code only in CRUD_TESTNET.

MGabala commented 1 year ago

Can you print the error message that returned from server?

As far as i know atm if i send Post Request:

{{url}}/api/v3/order

from postman i get the same response:

{ "code": -1102, "msg": "Mandatory parameter 'side' was not sent, was empty/null, or malformed." }

like this one from my API client.. How should i send these?

MGabala commented 1 year ago

It is about SHA256.