Łukasz Wiland
Łukasz Wiland

Reputation: 13

Download data from API [Python]

How can i download data from API, which look like this (sorry - maybe my informations are not clear but i have a beginer with API)

access-control-allow-headers: Authorization,User-Agent,Range,X-Requested-With,Content-Type,Partner
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-origin: https://test.deribit.com
cache-control: no-store
connection: keep-alive
content-length: 149
content-type: application/json
date: Fri, 04 Sep 2020 08:36:46 GMT
server: nginx/1.17.9
vary: Origin,Authorization,Partner
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{
  "jsonrpc": "2.0",
  "id": 11,
  "error": {
    "message": "unauthorized",
    "code": 13009
  },
  "testnet": true,
  "usIn": 1599208606258957,
  "usOut": 1599208606259032,
  "usDiff": 75
}

i tried to use sth like this:

import requests

payload = {
"jsonrpc": "2.0",
"id": 11,
"error": {
"message": "unauthorized",
"code": 13009
  },
"testnet": 'true',
"usIn": 1599208606258957,
"usOut": 1599208606259032,
"usDiff": 75
}
r=requests.get('https://test.deribit.com',data = payload)


r.json()

But i got an error. Can u help me with this problem?

Upvotes: 1

Views: 370

Answers (3)

nenadp
nenadp

Reputation: 816

You've received an empty response since JSON is unable to serialize it, probably HTTP status 400. You can check response code using response.status_code, that might help you find the cause of the error.

From your question, I can only assume you need to call POST (or maybe PUT) instead of GET, since you are sending payload, which is not standard for GET requests:

r=requests.post('https://test.deribit.com', data=payload)

Request body (your payload) is standard for POST (create an object using data from body), PUT/PATCH (Update object using data from request body). Post is sometimes also used to retrieve data, if there are too many parameters to fit in url.

Upvotes: 1

Ralle12
Ralle12

Reputation: 15

I think what you need is this. Happy coding :) If you have any questions, leave me a comment, then I will help you of the best of my ability :)

Python API Tutorial

Upvotes: 0

Łukasz Wiland
Łukasz Wiland

Reputation: 13

@Andy_101 yes, my error message is below:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-50-33c5e77ebc90> in <module>()
     16 
     17 
---> 18 r.json()

~\Anaconda3\lib\site-packages\requests\models.py in json(self, **kwargs)
    894                     # used.
    895                     pass
--> 896         return complexjson.loads(self.text, **kwargs)
    897 
    898     @property

~\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Upvotes: 0

Related Questions