Unable to access data from .env file in python3 - python-3.x

I am trying to write automation scripts for get request.
As part of the request i need to send secure key in headers. I am giving following details in test
url = "some url"
headers = {"key":"12345"}
response = requests.post(url, json_request, headers=headers)
At the moment i'm exposing header details. I am trying to get them saved to environ.env file and import them into config file. But i have no luck.
environ.env file
test_headers = {"key":"12345"}
config file
import os
headers = {"headers": os.environ.get("test_headers")}
Unable to get the code working. Headers are not recognised while executing code.
Any help would be greatly appreciated.
Thanks,

Got this to be working
environ.env file
KEY:"12345"
config file
import os
headers = {"headers": os.environ.get("KEY")}

Related

How to replay server response from .har file?

I am new to this, this is my first question here.
Please guide me to improve.
Learning python automation.
Came across scenario to automate "replay server response from .har file".
Could capture, HAR file
from browsermobproxy import Server
server = Server("~/browsermob-proxy")
server.start()
proxy = server.create_proxy()
from selenium import webdriver
co = webdriver.ChromeOptions()
co.add_argument('--proxy-server={host}:{port}'.format(host='localhost', port=proxy.port))
driver = webdriver.Chrome(executable_path = "~/chromedriver", chrome_options=co)
proxy.new_har
driver.get(url)
proxy.har
How can I replay server response from captured HAR file (offline) ?
Is it possible with python or any other ?
And what's the significance of replay server response ?
Could you please help me ?
Check out this https://github.com/louisabraham/har2requests. It should convert the .har file into a python script.

Making a PATCH method request with a file to Common Data Service

I'm trying to create a Python script which can use the PATCH method to upload a file into MS's Common data service. I'm successfully making GET, POST, PATCH, and DELETE calls with simple data, but have so far been unable to configure it so that I can upload a file.
I've been using the Requests library for Python, with the requests.patch function in order to try updating the data. I'm attempting to upload a .csv file into the field, the file which i'm uploading has a filesize of 1kb.
If I upload the data directly into the common data service through the in-built data interface, my browser is able to correctly make a PATCH call. I've attempted to copy the call as closely as I can, but have had zero success.
File field in common data service
PATCH call in web browser
What is the correct way to make a PATCH request with a file to Microsoft's Common data service?
Made a mistake with the url in my request - I had missed out which field I was uploading data to
Incorrect URL:
https://90g9j3gf.crm4.dynamics.com/api/data/v9.0/test_entity(34cd854c-1175-4778-bf95-e1ce12dea3b0)
Corrected URL:
https://90g9j3gf.crm4.dynamics.com/api/data/v9.0/test_entity(34cd854c-1175-4778-bf95-e1ce12dea3b0)/test_field
The code I used to make the request:
Import requests
http_headers = {
'Authorization': 'Bearer ' + token['access_token'],
'Content-Type': 'application/octet-stream',
'x-ms-file-name': 'test.csv'
}
filedata = open("project-folder\\test.csv", "rb")
patch_req = requests.patch(
url, # My URL is defined elsewhere
headers=http_headers,
data=filedata
)
This now works correctly for me

PRIVACY Card API Authorization

I have recently been working with API's but I am stuck on one thing and it's been holding me back for a few days.
I am trying to work with Privacy's API and I do not understand the Authentication/Authorization process. When I enter the url in a browser I get the error "message": "Please provide API key in Authorization header", even when I use the correct format of Authorization. I also get an error when I make a request in Python. The format I'm using for the url is https://api.privacy.com/v1/card "Authorization: api-key:".
If someone could explain how to work this or simply give an example of how I would make a request through Python3. The API information is in the link below.
Thank you in advance.
https://developer.privacy.com/docs
This is the code I am using in Python. After I run this I receive a 401 status code.
import requests
headers={'Authorization': 'api-key:200e6036-6894-xxxx-xxxx-xxxx'}
url = 'https://api.privacy.com/v1/card'
r = requests.get(url)
print("Status code:", r.status_code)
You need to add the authentication header to the get call. It isn't enough to include it in a header variable. You need to provide those headers to requests
import requests
response = requests.get('https://api.privacy.com/v1/card', headers={'Authorization': 'api-key 65a9566c-XXXXXXXXXXXX'})
print(response.json())

using requests.post to send a request with a certificate

I am trying to send some data via a web based api. The client has given me an example snippet of code to test everything.
My problem below is how to use my certificate.
Their example
response = requests.post(url, data=data, headers=headers, cert=('certificate.cer','keyfile.key'))
I tried
response = requests.post(url, data=data, headers=headers, cert=('C:\MyPath\My-certs.p12','password'))
However I get the error,
OSError: Could not find the TLS key file, invalid path: password
I have written a C# example and the code works, so I know the certificate and the other pieces are ok. I just can't get the certificate to work in python
Your certificate file is P12, which isn't supported by requests yet. You have two options:
Convert the P12 file into separate public certificate and private key files. For Linux or Mac, use this. Then you can use the separate files as a tuple in cert.
Use requests-pkcs12 which has support for P12 files:
--
from requests_pkcs12 import post
response = post(url, data=data, headers=headers, pkcs12_filename='C:\MyPath\My-certs.p12', pkcs12_password='password')

urlopen(url) 403 Forbidden error

I'm using python to open an URL with the following code and sometimes I get this error:
from urllib import urlopen
url = "http://www.gutenberg.org/files/2554/2554.txt"
raw = urlopen(url).read()
error:'\n\n403 Forbidden\n\nForbidden\nYou don\'t have permission to access /files/2554/2554.txt\non this server.\n\nApache Server at www.gutenberg.org Port 80\n\n'
What is this?
Thank you
This is the web page blocking Python access as it is making requests with the header 'User-Agent'.
To get around this, download the 'urllib2' module and use this code:
req = urllib2.Request(url, headers ={'User-Agent':'Chrome'})
raw = urllib2.urlopen(req).read()
You are know accessing the site with the header 'Chrome' and should no longer be forbidden (I tried it myself and it worked).
Hope this helps.

Resources