This seems like a stupid question, and probably is.
Say that I'm making a request to an API as follows:
r = requests.post("http://EXAMPLE.COM/api/randomstring")
## I've blocked out the real URL so false requests aren't made as it's publicly available
The specification for this API is that on every request, it generates a random string of 10 letters. If the request is processed successfully, the server should respond with 201 CREATED and a text/plain payload giving the random string from the API database.
I have previously tried the following to parse the request, and add it to a locally stored database model:
new_word = r.text()
Words.objects.create(word=new_word)
But this gave the error:
TypeError: 'str' object is not callable
I then tried simply:
Words.objects.create(word=r)
When I run this from a client, and then check the local database, it only shows as:
word: <Response [201]>
This means that obviously the request was successful, and a word is in the text/plain payload.
But how do I parse only the word from the payload, so that I am able to save it as (for example):
word: hskcudyhft
I know how to parse a JSON payload which is probably much trickier than this - which is probably why I'm overthinking it!
Many thanks!
response = requests.post("http://EXAMPLE.COM/api/randomstring")
In order access Response Content, you should use response.text
, as text
is an attribute of response object.
So change,
new_word = r.text()
To
new_word = r.text
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.