requests.exceptions.MissingSchema: Invalid URL Python API Get request - python

I am trying to pull the data using get request through the below URL and I'm getting below error.
I'm not understanding what is the issue with the URL.
Any help would be appreciated
import requests
from requests.auth import HTTPBasicAuth
import json
import urllib.parse
url = """https://msi.abc.com/admin/ui/feedbackCSV/reports
/5d14de32309baf0001501fb7 ?reports[]=pageviews&reports[]=
searches&from=Oct 1 2020&to=Oct 2 2020"""
encode_url = urllib.parse.quote(url,encoding='utf-8')
response = requests.get(encode_url,auth = HTTPBasicAuth('admin#abc.com', 'Summer2020'))
print(response.content)
Error
raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL 'https%3A%2F%2Fmsi.abc.com%2Fadmin%2Fui%2FfeedbackCSV%2Freports%0D%0A%2F5d14de32309baf0001501fb7%20%3Freports%5B%5D%3Dpageviews%26reports%5B%5D%3D%0D%0Asearches%26from%3DOct%201%202020%26to%3DOct%202%202020': No schema supplied. Perhaps you meant http:https%3A%2F%2Fmsi.abc.com%2Fadmin%2Fui%2FfeedbackCSV%2Freports%0D%0A%2F5d14de32309baf0001501fb7%20%3Freports%5B%5D%3Dpageviews%26reports%5B%5D%3D%0D%0Asearches%26from%3DOct%201%202020%26to%3DOct%202%202020?

This might be due to that you pass the whole URL to the urllib.parse.quote function. Try passing the params only to the urllib.parse.quote or use requests params like in the below example:
import requests
from requests.auth import HTTPBasicAuth
import json
import urllib.parse
url = "https://msi.abc.com/admin/ui/feedbackCSV/reports/5d14de32309baf0001501fb7"
payload = {'reports[]':'pageviews', 'reports[]':'searches','from':'Oct 1 2020', 'to':'Oct 2 2020' }
authParams = HTTPBasicAuth('admin#abc.com', 'Summer2020')
response = requests.get(url,params=payload, auth =authParams )
print(response.content)

Related

Error in POST API python while trying to send data

Here is the json data to send
{
"mobile": "1234567890"
}
Here is the python code:
from urllib import response
import requests
import urllib
import json
query = {"mobile": "1234567890"}
headers = {"Content-Type":"application/json"}
url = 'https://cdn-api.co-vin.in/api/v2/auth/public/generateOTP'
y = requests.post(url, data=query, headers=headers)
print(y)
Response from the API:
c:/pythonprojects/covid-otp-requests.py
<Response [400]>
I am a newbie, I don't understand the mistake I am making, Can someone please help me out?.
Fix here:
from urllib import response
import requests
import urllib
import json
params = {
"mobile": "1234567890"
}
headers = {"Content-Type":"application/json"}
response = requests.post("https://cdn-api.co-vin.in/api/v2/auth/public/generateOTP", data=json.dumps(params))
print(response)
details = response.json()
print(details)
The parameters has to be sent in json format which I making a mistake in.
Thank you all!

How to get all request from API with rate limiting using python

I am trying to get all request of API with rate limit of 100 request per minute. This API request is in json format. I used import urllib, import requests and import json and from time import sleep. My code below returns the same data
import urllib
import requests
import json
import urllib.request as urllib2
from time import sleep
data = []
#resp
for i in range(4):
url = "https://api.leadfeed.com/accounts/*?start_date=2018-01-01&end_date=2020-06-01"
header = {"Authorization": 'Bearer GnHbmjtUEQDXCFOlwKAyy9hJedZtDg'}
resp = requests.get(url, headers = header)
#print(i)
data.append(resp.text)
sleep(60)
print(data)
It returns 4 the same json data as shown below:
[
"{\"data\":[{\"id\":\"b50b0cf4-06f1-11ea_p6q3nDbB2C4m9KjK9gndy\",\"type\.....}"
"{\"data\":[{\"id\":\"b50b0cf4-06f1-11ea_p6q3nDbB2C4m9KjK9gndy\",\"type\.....}"
"{\"data\":[{\"id\":\"b50b0cf4-06f1-11ea_p6q3nDbB2C4m9KjK9gndy\",\"type\.....}"
"{\"data\":[{\"id\":\"b50b0cf4-06f1-11ea_p6q3nDbB2C4m9KjK9gndy\",\"type\.....}"
]
I don't know the length of the json that why i used range(4) to return 4 json data.
How can i get data from API with rate limit request?

How to set params in Python requests library

I have the following code using urllib in Python 2.7 and its working. I'm trying to do the same request using the requests library but I cant get it to work.
import urllib
import urllib2
import json
req = urllib2.Request(url='https://testone.limequery.com/index.php/admin/remotecontrol',\
data='{\"method\":\"get_session_key\",\"params\":[\"username\",\"password\"],\"id\":1}')
req.add_header('content-type', 'application/json')
req.add_header('connection', 'Keep-Alive')
f = urllib2.urlopen(req)
myretun = f.read()
j=json.loads(myretun)
print(j['result'])
Using requests library( Doesn't work)
import requests
import json
d= {"method":"get_session_key","params":["username","password"],"id":"1"}
headers = {'content-type' :'application/json','connection': 'Keep-Alive'}
req2 = requests.get(url='https://testone.limequery.com/index.php/admin/remotecontrol',data=d,headers=headers)
json_data = json.loads(req2.text)
print(json data['result'])
I'm getting an error JSONDecodeError: Expecting value: line 1 column 1 (char 0) How can I make the code work with the requests library?
First, you're sending the wrong type of request. You're sending a GET request, but you need to send a POST, with requests.post.
Second, passing a dict as data will form-encode the data rather than JSON-encoding it. If you want to use JSON in your request body, use the json argument, not data:
requests.post(url=..., json=d)
Reference Link: http://docs.python-requests.org/en/master/api/
You can use requests module of python like so
import requests
Req = requests.request(
method = "GET", # or "POST", "PUT", "DELETE", "PATCH" etcetera
url = "http(s)://*",
params = {"key": "value"}, # IF GET Request (Optional)
data = {"key": "value"}, # IF POST Request (Optional)
headers = {"header_name": "header_value"} # (Optional)
)
print Req.content
You can surround the code with try::catch block like below to catch any exception thrown by requests module
try:
# requests.request(** Arguments)
except requests.exceptions.RequestException as e:
print e
For full argument list, please check reference link.

How to get cookies from urllib.request?

How to get cookie from an urllib.request?
import urllib.request
import urllib.parse
data = urllib.parse.urlencode({
'user': 'user',
'pass': 'pass'
})
data = data.encode('utf-8')
request = urllib.request.urlopen('http://example.com', data)
print(request.info())
request.info() returns cookies but not in very usable way.
response.info() is a dict type object. so you can parse any info you need. Here is a demo written in python3:
from urllib import request
from urllib.error import HTTPError
# declare url, header_params
req = request.Request(url, data=None, headers=header_params, method='GET')
try:
response = request.urlopen(req)
cookie = response.info().get_all('Set-Cookie')
content_type = response.info()['Content-Type']
except HTTPError as err:
print("err status: {0}".format(err))
return
You can now, parse cookie variable as your application requirement.
Just used the following code to get cookie from Python Challenge #17, hope it helps (Python 3.8 being used):
import http.cookiejar
import urllib
cookiejar = http.cookiejar.CookieJar()
cookieproc = urllib.request.HTTPCookieProcessor(cookiejar)
opener = urllib.request.build_opener(cookieproc)
response = opener.open(url)
for cookie in cookiejar:
print(cookie.name, cookie.value)
I think using the requests package is a much better choice these days. Try this sample code that shows google setting cookies when you visit:
import requests
url = "http://www.google.com"
r = requests.get(url,timeout=5)
if r.status_code == 200:
for cookie in r.cookies:
print(cookie) # Use "print cookie" if you use Python 2.
Gives:
Cookie NID=67=n0l3ME1Jl3-wwlH7oE5pvxJ_CfU12hT5Kh65wh21bvE3hrKFAo1sJVj_UcuLCr76Ubi3yxENROaYNEitdgW4IttL43YZGlf8xAPl1IbzoLG31KP5U2tiP2y4DzVOJ2fA for .google.se/
Cookie PREF=ID=ce66d1288fc0d977:FF=0:TM=1407525509:LM=1407525509:S=LxQv7q8fju-iHJPZ for .google.se/

Twitter Request token always fails with 401 in python

I am using urllib and urllib2 to get request token from twitter as recommended here https://dev.twitter.com/docs/auth/implementing-sign-twitter, code is below. Can anyone see, what am I doing wrong? I get 'urllib2.HTTPError: HTTP Error 401: Unauthorized'
#! env python
import sys
import os
import urllib
import urllib2
import uuid
import time
import base64
import string
# 1. Obtain a request token
# 2. Redirect the user
# 3. Convert the request token to an access token
consumer_key= "xxxxxxx"
consumer_secret= "xxxxxxxxxxxxxxx"
# Request token URL: https://api.twitter.com/oauth/request_token
# Authorize URL: https://api.twitter.com/oauth/authorize
# Access token URL: https://api.twitter.com/oauth/access_token
nonce = base64.b32encode(str(uuid.uuid4().hex))
# example: MYZTMMRYMEYDQN3FMFSDIYJXGNRDCMBWG42TAMJUGVTGGMZRMFSA====
nonce = nonce.rstrip('=')
print 'Nonce', nonce
data = { 'oauth_callback' : 'http://www.gooogle.com',
'oauth_consumer_key' : 'bwKT66akiJSin30L1mGnQ',
'oauth_nonce' : nonce,
'oauth_timestamp' : int(time.time()),
'oauth_version' : "1.1"
}
url_values = urllib.urlencode(data)
url = 'https://api.twitter.com/oauth/request_token'
full= url + '?' + url_values
data = urllib2.urlopen(full)
Which all query parameters are mandatory here?

Categories

Resources