# Requests Library ## `import requests` This is how we can send requests to the server we have running ### If you want to test this: * You have to make sure you have a server running the command for this requires you to: 1. Navigate to the folder that contains paths to both the src files and the test files 2. Run: `python3 -m (folder that has the server file).(file that has the server)` 3. This should result in a flask server running notification on your screen - when you see this you're good to start testing! ### Testing methods #### Get This will need your server file to have something like the following: `data = request.args.get('data')` To test this your code will look like the following: `resp = requests.get(f'{URL}/whateverroute', params=data)` * resp is the response that requests receives after inputing the data that you are sending to the server * requests.get is what we use to send date to a route that is of GET method * params is the format we send data to GET requests as - this is simulating putting this in the query string in ARC server * data is the data you have inputed in a dictionary format (because json is a string that is formatted like a dictionary) #### Put This will need your server file to have something like the following: ``` `data = request.get_json()` ``` To test this your code will look like the following: ``` `resp = requests.put(f'{URL}/whateverroute', json=data)` ``` * resp is the response that requests receives after inputing the data that you are sending to the server * requests.put is what we use to send date to a route that is of PUT method * json is the format we send data to PUT requests * data is the data you have inputed in a dictionary format (because json is a string that is formatted like a dictionary) #### Post This will need your server file to have something like the following: ``` data = request.get_json() ``` To test the following you will need to have this in your test ``` resp = requests.put(f'{URL}/whateverroute', json=data) ``` * resp is the response that requests receives after inputing the data that you are sending to the server * requests.post is what we use to send date to a route that is of POST method * json is the format we send data to POST requests * data is the data you have inputed in a dictionary format (because json is a string that is formatted like a dictionary) #### Delete This will need your server file to have something like the following: ``` * data = request.get_json() ``` To test the following you will need to have this in your test ``` resp = requests.put(f'{URL}/whateverroute', json=data) ``` * resp is the response that requests receives after inputing the data that you are sending to the server * requests.delete is what we use to send date to a route that is of DELETE method * json is the format we send data to DELETE requests * data is the data you have inputed in a dictionary format (because json is a string that is formatted like a dictionary) ### The response object The response object allows to check a whole bunch of things - the most relevant to you: `resp.status_code` - this shows you whether the request you sent was accepted or caused an error, typical codes that you might see are: * 200 - everythng worked * 500 - server error * 400 - inputerror * 403 - accesserror We can convert the response object into a json file so that we are able to manipulate it like we do in our regular tests by using the following code: `load = resp.json()` * load is now the response in json form that can be manipulated in python (e.g. we can now use loops to look for particular keys in the dictionary) * json is the resopnse format that we received from the server