简体   繁体   中英

How can I find ID of new record? Python, request, postgresql, curl, create record and answer

How can I get ID of new record my code:

a = requests.post(url_post, headers=headers, data=new_record)

print(f"status code: {a.status_code}\ntext: {a.text}\nrequest: {a.request}\nheader: {a.headers}\n"
              f"content: {a.content}\nraw: {a.raw}\nencoding: {a.encoding}\n"
              f"json: -> Exception TYPE: <class 'simplejson.errors.JSONDecodeError'>\n"
              f"reason: {a.reason}\n"
              f"raise for status: {a.raise_for_status()}\n"
              f"links: {a.links}\n"
              f"history: {a.history}\n"
              f"cookies: {a.cookies}\n"
              f"elapsed: {a.elapsed}\n"
              f"next: {a.next}")

What I get:

status code: 201
text: 
request: <PreparedRequest [POST]>
header: {'Transfer-Encoding': 'chunked', 'Date': 'Wed, 20 Jul 2022 09:14:40 GMT', 'Server': 'postgrest/9.0.1.20220717 (pre-release)', 'Content-Range': '*/*'}
content: b''
raw: <urllib3.response.HTTPResponse object at 0x7f3eb9a2e6d0> 
encoding: None
json: -> Exception TYPE: <class 'simplejson.errors.JSONDecodeError'>
reason: Created
raise for status: None
links: {}
history: []
cookies: <RequestsCookieJar[]>
elapsed: 0:00:00.001559
next: None

I am new hier, please don't be angry to me. I am trying to write in right way. Thank you for your patience.

headers = {'Authorization': self.__token,
                   'accept': 'application/json',
                   'Prefer': 'return=representation'
                   }

It is helping to get a more information:

'Prefer': 'return=representation'

link to explaining: http://postgrest.org/en/v5.2/api.html#insertions-updates .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM