Python HTTP Client GET issue with port - python-3.x

I am trying to GET response from a login page
Here is the code i tried to use
import http.client
url ="https://accounts.spotify.com/en/login?continue=https:%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F"
conn = http.client.HTTPSConnection(url,port=None)
conn.request("GET", "/")
r1 = conn.getresponse()
print(r1.status, r1.reason)
And this is the error i am getting
InvalidURL: nonnumeric port: '%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F'
The get url in my code contains : its reading data after that as PORT even after passing an extra argument port=None
Can someone help me out of this

You have to just mention the host name and port in the constructor(
Read more here).
Rest of the request can be given in the request.You can view a lot of examples in the docs page.
You could pass the host name and rest of the request like this.
import http.client
url ="accounts.spotify.com"
conn = http.client.HTTPSConnection(url,port=None)
conn.request("GET", "/en/login?continue=https:%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F")
r1 = conn.getresponse()
print(r1.status, r1.reason)
Output
200 OK

Related

Unauthorized Python Requests using Mohawk

I'm trying to get a POST requests through python. Server side dev provided me with an auth key ie: short124 and a long key. They were generated by Mohawk library. Dev from server side gave me a code example:
import requests
from mohawk import Sender
creds = {'id': short,'key':very_long_key,'algorithm':'sha256'}
auth_session = Sender( credentials = creds, url = url, method = 'POST',always_hash_content=False)
headers = {'Authorization' : auth_session.request_header}
r = requests.post(url,data = input, headers = headers, verify = False)
print(r)
Unfortunately I get 401 error (unauthorized) with the credentials provided,
Can anyone give me some advide?
The way it's being used seem correct. I would double check the credentials and the url.
Also consider using Black it will make code formatting a breeze.

Add headers and payload in requests.put

I am trying to use requests in a Python3 script to update a deploy key in a gitlab project with write access. Unfortunately, I am receiving a 404 error when trying to connect via the requests module. The code is below:
project_url = str(url)+('/deploy_keys/')+str(DEPLOY_KEY_ID)
headers = {"PRIVATE-TOKEN" : "REDACTED"}
payload = {"can_push" : "true"}
r = requests.put(project_url, headers=headers, json=payload)
print(r)
Is there something that I am doing wrong where in the syntax of my Private Key/headers?
I have gone through gitlab api and requests documentation. I have also confirmed that my Private Token is working outside of the script.
I am expecting it to update the deploy key with write access, but am receiving a upon exit, making me think the issue is with the headers/auth.
This is resolved, it was actually not an auth problem. You must use the project ID instead of url, for example:
project_url = f'https://git.REDACTED.com/api/v4/projects/{project_id}/deploy_keys/{DEPLOY_KEY_ID}'
headers = {"PRIVATE-TOKEN" : "REDACTED"}
payload = {'can_push' : 'true'}
try:
r = requests.put(project_url, headers=headers, data=payload)

MissingSchema - Invalid URL Error with Requests module

Trying to get data from the eBay API using GetItem. But requests isn't reading or getting the URL properly, any ideas? Getting this error:
requests.exceptions.MissingSchema: Invalid URL '<urllib.request.Request object at 0x00E86210>': No schema supplied. Perhaps you meant http://<urllib.request.Request object at 0x00E86210>?
I swear I had this code working before but now it's not, so I'm not sure why.
My code:
from urllib import request as urllib
import requests
url = 'https://api.ebay.com/ws/api.dll'
data = '''<?xml version="1.0" encoding="utf-8"?>
<GetItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>my-auth-token</eBayAuthToken>
</RequesterCredentials>
<ItemID>any-item-id</ItemID>
</GetItemRequest>'''
headers = {
'Content-Type' : 'text/xml',
'X-EBAY-API-COMPATIBILITY-LEVEL' : 719,
'X-EBAY-API-DEV-NAME' : 'dev-name',
'X-EBAY-API-APP-NAME' : 'app-name',
'X-EBAY-API-CERT-NAME' : 'cert-name',
'X-EBAY-API-SITEID' : 3,
'X-EBAY-API-CALL-NAME' : 'GetItem',
}
req = urllib.Request(url, data, headers)
resp = requests.get(req)
content = resp.read()
print(content)
Thank you in advance. Any good reading material for urllib would be great, too.
You are mixing the urllib and the requests library. They are different libraries that can both do HTTP-requests in Python. I'd suggest you use only the requests library.
Remove the line req = urllib.Request(url, data, headers) and replace the resp = ... line with:
r = requests.post(url, data=data, headers=headers)
Print the response body like this:
print(r.text)
Check out the Requests Quickstart here for more examples: https://2.python-requests.org//en/master/user/quickstart/

Hanging delete request

Below returns all the info for the dashboards belonging to vhost 'test'
import requests
url = 'https://abcde/api/dashboards
headers = {'vhost' : 'test'}
r = requests.get(url, headers=headers, auth=('user' , '1234'))
print(r.text)
Now I would like to delete all the dashboards but what I have below hangs for minutes until I break out of it. The dashboards are still there viewed from the UI. I have no baseline as to how long a delete should take.
import requests
url = 'https://abcde/api/dashboards
headers = {'vhost' : 'test'}
r = requests.delete(url, headers=headers, auth=('test' , '1234'))
print(r.text)
Thank you
Using Postman, I received a 500 error
While the server was building the response a unexpected internal server error occured which forced the server to abort the request

python3 -- getting POST request text from requests.post?

I'm using python3. For debugging purposes, I'd like to get the exact text that is sent to the remote server via a requests.post command. The requests.post object seems to contain the response text, but I'm looking for the request text.
For example ...
import requests
import json
headers = {'User-Agent': 'Mozilla/5.0'}
payload = json.dumps({'abc':'def','ghi':'jkl'})
url = 'http://example.com/site.php'
r = requests.post(url, headers=headers, data=payload)
How do I get the text of the exact request that is sent to url via this POST command?

Resources