Python Forum
How to save Python Requests data sent to server?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save Python Requests data sent to server?
#1
Hi, Smile

Firstly, I am using Python 3.4, Flask and Python Anywhere server.

So, I am using Requests to send data to the server. This seems to work ok as a practice script but I'm having problems in saving the data to be able to observe it later. For now, just saving it in a basic text file would be fine.

Script (sent from Python Idle on laptop):

import requests
import json

response = requests.post('https://madmartin.pythonanywhere.com/postit', json={
    "language" : "German",
    "framework" : "Flask",
    
    "version_info" : {
        "python" : 3.4,
        "flask" : 0.12
    },
    "examples" : ["query", "form", "json"],
    "boolean_test" : True
})
response.status_code
   
if response.status_code == 200:
    print('Success- payload delivered!2020')
    print(response.content)
else:
    print('Got unexpected status code {}: {!r}'.format(response.status_code, response.headers))

Now, I'm not even sure it's possible to SAVE that data in the server as I've not added in any kind of password, token etc, but on sending that code, the headers are returned correctly (in Idle), as such:

Success- payload delivered!2020
b'\n           The framework value is: German\n           The language value is: Flask'
on the server (in flask_app.py) I have this code as the view function:

@app.route('/postit', methods=['GET', 'POST'])
def postit():

    req_data = request.get_json()
    language = req_data['language']
    framework = req_data['framework']
    python_version = req_data['version_info']['python'] #two keys are needed because of the nested object
    example = req_data['examples'][0] #an index is needed because of the array
    boolean_test = req_data['boolean_test']


    POST={}
    #args=sys.stdin.read()
    x = POST.get('framework')

    with open('/home/MadMartin/datapostfiles/EFL4.json', 'w') as f:
        f.write(str(x))
        f.close()


    return '''
           The framework value is: {}
           The language value is: {}'''.format(language, framework, x)
However, all this does is save an empty file(EFL4.json) - albeit with 'None' written in it.

So, is there any way to acutally save 'German' and 'Flask' on the server, or whatever is sent to it from Idle?

thanks in advance for any help offered
Reply


Messages In This Thread
How to save Python Requests data sent to server? - by RedLeonard - Jun-30-2020, 11:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 542 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  python requests library .JSON() error mHosseinDS86 6 3,400 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  In SQL Server, mix data types. shiv11 0 876 Sep-21-2022, 12:50 PM
Last Post: shiv11
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,214 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Save data frame to .csv df.to.csv() mcva 1 1,525 Feb-03-2022, 07:05 PM
Last Post: mcva
  Python requests oauth2 access token herobpv 6 3,894 Sep-27-2021, 06:54 PM
Last Post: herobpv
  How to take the tar backup files form remote server to local server sivareddy 0 1,895 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,151 Jul-02-2021, 02:19 PM
Last Post: xtc14
  How to save json data in a dataframe shantanu97 1 2,150 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Python Requests SSL Aussie 0 1,990 Jan-07-2021, 02:09 AM
Last Post: Aussie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020