G
G
GineTik2021-01-01 13:39:57
Python
GineTik, 2021-01-01 13:39:57

Sending a request with the same body gives different results, how to fix it?

I form the request body like this:

price = self.get_price()
body = '{' + f'"currency_pair":"{pair}","type":"limit","account":"spot","side":"{side}","amount":"{quantity}","price":"{price}","time_in_force":"gtc"' + '}'


line in the console looks like this:
{"currency_pair":"BNB_USDT","type":"limit","account":"spot","side":"buy","amount":"0.019",
"price":"516.2708"," time_in_force":"gtc"}


a request with such a body returns the following response: 
{'status': 405, 'label': 'METHOD_NOT_ALLOWED', 'message': ''}


if we slightly change the price assignment: 
price = 516.2708 # self.get_price()
body = '{' + f'"currency_pair":"{pair}","type":"limit","account":"spot","side":"{side}","amount":"{quantity}","price":"{price}","time_in_force":"gtc"' + '}'


line looks identical to the previous one:
{"currency_pair":"BNB_USDT","type":"limit","account":"spot","side":"buy","amount":"0.019",
"price":"516.2708"," time_in_force":"gtc"}


answer:
{'id': '109317501746', 'text': 'apiv4', 'create_time': '1641032772', 'update_time': '1641032772', 'create_time_ms': 1641032772621, ... }


the function get_price()takes the price string from the site, converts it to a float and returns the value
host = "https://api.gateio.ws"
    prefix = "/api/v4"
    headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
    url = ''

    def get_url(self, param=None, host=None):
        if host is None:
            host = self.host

        if param is None:
            return host + self.prefix + self.url
        return host + self.prefix + self.url + "?" + param

    def get_price(self) -> float:
        return float(self.get_price_by_str())

    def get_price_by_str(self):
        self.url = '/spot/order_book'
        query_param = f'currency_pair={self.pair}'
        r = self.send_request(query_param)
        print(r)
        return r["bids"][0][0]

    def send_request(self, param: str = None, body: dict = None, method='GET', authorization=False):
        headers = self.headers
        data = None
        if body is not None:
            data = json.dumps(body)

        if authorization is True:
            headers.update(self.gen_sign(method, param, data))

        r = requests.request(method, self.get_url(param), headers=headers, data=data)
        return r.json()


the problem is that I have to dynamically get the price and I can’t assign a static value to a variable

if something is not clear I will be happy to add more information

I will be grateful for any hint / solution :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alekssamos, 2022-01-03
@alekssamos

import json
body = json.dumps({"currency_pair":pair, "type":"limit", "account": "spot", "side": side, "amount":quantity, "price":price, "time_in_force": "gtc"})

There is a difference. Compare:
>>> type("2.46");  type(2.46)
<class 'str'>
<class 'float'>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question