How to write http response to a file
Write or append HTTP response text to a file in Python using the requests library.
For text response: use response.text
response = requests.get("https://api.github.com/users/saadjs")
with open("response.txt", "w") as f: # 'w' for write file permission
f.write(response.text)
To append to a file:
response = requests.get("https://api.github.com/users/saadjs")
with open("response.txt", "a") as f: # 'a' for append file permission
f.write(response.text)
Related posts
- List of Useful Python Tricks for Solving Coding QuestionsOctober 4, 2023
- Python: useful built-in FunctionsAugust 17, 2023
- How to create Python virtual environmentJanuary 9, 2023