Making GET requests from URLs with flask - python

Making GET requests from urls can be done with the python requests library like this:
import requests
response = requests.get("https://someapi.io/api/data")
response.json()
I'm currently trying to make GET requests from URLs with the flask request module but i can't find any useful information.

Flask is a server, the request object you can access on flask.request is the context of the request you received on the related API request.
Please see: https://flask.palletsprojects.com/en/1.1.x/
The requests library is not related and with this one, you can do all the GET requests you want.
You can do a request in one of your API requests handled by your flask server but you would have to use directly the request module and not the request object of the flask module.

Related

Same identical response, rejected in Scrapy, accepted in Requests

I'm trying to scrape a web site. Before writing the code, I copied the request from the browser's network tool as a curl and pasted it into Insomnia (like Postman). I could resend the same request with an 200 response in Insomnia.
However, when I mimic the same request in a scrapy Request with the same body, url and header; Scrapy receives 403 responses. On the other hand, if I populate a Python code with requests library through Insomnia; the identical request works in requests library.
So how could two identical requests sent in Scrapy and Requests have different responses, that is, the one Scrapy send fails whereas the one Requests sents succeeds?
Thank you

API requests always return 404 no matter what

I am trying to automate the trading strategy that I executed manually before. This requires communicating with my broker through an API. I am authorizing through HTTP basic auth. To test I tried to make an API request to get information about funds in my account.
At first, I was getting 401 responses and it turned out that I was using the wrong identification information.
After I fixed this issue, all API requests that I am making are giving me 404 responses.
An example
import requests
from requests.auth import HTTPBasicAuth
response = requests.get("https://api-demo.exante.eu/md/{version}/accounts", auth=HTTPBasicAuth
('name', 'pass'))
print(response)
After this, I tried some code online to check whether or not there are other problems. I tried this
https://gist.github.com/rshrc/127ba2c20df74263d71bc5a5595c8969
and this also gives me 404.
Link to my brokers API documentation:
https://api-live.exante.eu/api-docs/#section/API-versions
Does anyone know where might be the problem? Directions would be helpful. Thanks!
It looks like you're not passing a version with the {version} variable. Don't forget to also format the URL string with an f before it. This should work:
import requests
from requests.auth import HTTPBasicAuth
version = "3.0"
response = requests.get(f"https://api-demo.exante.eu/md/{version}/accounts", auth=HTTPBasicAuth
('name', 'pass'))
print(response)

Responding to an http request with JSON in Python

I have several Python scripts that are used from the CLI. Now I have been asked if I could provide a web API to perform some of the work normally done from the CLI. I would like to respond with JSON formatted data. I know little about JSON or API's.
How do I retrieve the query data from the http request?
How to a create the HTTP headers for the response from my script?
Given the following URL, how can I respond with a JSON reply of the name "Steve"?
http://myserver.com/api.py?query=who
I already have a functioning Apache web server running, and can serve up HTML and CGI scripts. It's simply coding the Python script that I need some help with. I would prefer not to use a framework, like Flask.
A simple code example would be appreciated.
The following is the Python code that I've come up with, but I know it's not the correct way to do this, and it doesn't use JSON:
#!/usr/local/bin/python3.7
import cgi # CGI module
# Set page headers and start page
print("Content-type: text/plain")
print("X-Content-Type-Options: nosniff\x0d\x0a\x0d\x0a")
# Form defaults
query_arg = None
# Get form values, if any
form = cgi.FieldStorage()
# Get the rest of the values if the form was submitted
if "query" in form.keys():
query_arg = form["query"].value
if query_arg == "who":
print("Steve", end="", flush=True)
You are trying to build a request handler with core python code. which is able to handle http request, In my opinion its not good idea as there are multiple securty scenarios attached with it, also in cross server request its bit difficult to handle all request and request scenarios . I will suggest to use Flask which is very lightweight and this will give an pre-setup of routing mechanism to handle all kind of request in very less code below is the snippet to generate http json response hope it helps
import sys
import flask
import random, string
from flask import jsonify
class Utils:
def make_response(self):
response = jsonify({
'message': 'success',
})
response.status_code = 200
return response

vcrpy record cassettes doesn't record in a test using test_client.post

I'm implementing pytest and I want to record the answer of a request using vcrpy.
I'm using flask 1.0.2, pytest 4.5.0, pytest-flask 0.15.0 and vcrpy 2.0.1. I implemented a restful api using flask. I wrote a test but I would like to record the response of the request and vcr is not recording the response.
My endpoint is:
from flask import jsonify
from flask import Blueprint
class CompanyResource(Resource):
def post(self):
response = jsonify({
"timestamp": datetime.utcnow()
})
response.status_code = 201
return response
api_bp = Blueprint('dashboard', __name__)
api = Api(api_bp)
api.add_resource(CompanyResource, 'company', endpoint='company')
The test is the following
import pytest
import vcr
from flask import url_for
base_vcr = vcr.VCR(
cassette_library_dir='tests/dashboard/fixtures',
record_mode='once'
)
def test_create_company(client, company_data, headers):
with base_vcr.use_cassette('create_user.yaml'):
response = client.post(url_for('dashboard.company'),
json=company_data,
headers=headers)
I expect a file auto-generated on the directory specified. But when I run the test the cassette was not generated. The thing is that if I change client.post by requests.post the response is recorded for a get request, but if I try with a post an error is yield:
requests.exceptions.MissingSchema: Invalid URL '/api/dashboard/company': No schema supplied. Perhaps you meant http:///api/dashboard/company?
Do you have any suggestions to test my post endpoint and record the answer?
This is a little late but vcrpy has a list of libraries it records the HTTP requests for. requests is one of them, and the others are mentioned in https://vcrpy.readthedocs.io/en/latest/installation.html#compatibility.
aiohttp
boto
boto3
http.client
httplib2
requests (both 1.x and 2.x versions)
tornado.httpclient
urllib2
urllib3
Not sure what test client fixture you are using in your pytest, but if you use requests to call your endpoint successfully, it should record the response for you.

Print a formatted POST request URL using Python requests

I have a remote API I hit with POST requests. I have two scripts that do this job. One works; the other I need to debug.
The one that works is more complex and in Python, using the requests package:
import requests
from requests.auth import HTTPBasicAuth
r = requests.post("http://thing.com/",
data = {"param1": "foo"},
auth = HTTPBasicAuth("user#domain.com", "password"))
This works great. The other script, which is CLI, does theoretically the identical freaking thing and does not work. The CLI script prints out the URL it's trying to use. I'd love to get the working Python script to tell me which the hell URL it is trying to use. How do I make requests tell me the fully formatted URL it's using?
You can add the following to print out debug info about all your requests:
import logging
logging.basicConfig(level=logging.DEBUG)
You'll get output like
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): <URL>
DEBUG:requests.packages.urllib3.connectionpool:"POST /oauth/token HTTP/1.1" 200 None
If you want to print other information, you can print out the objects you're passing in or implement some of the requests event hooks.

Categories

Resources