Can't pass base 64 to post request python - python

enter image description here
Hi i am trying to pass base64 image to post api request but getting error please help i am new to programming
import requests
import json, base64
final_list = []
def vehicle_damage(img):
url = "https://vehicle-damage-assessment.p.rapidapi.com/run"
payload = {
"draw_result": True,
"image": img
}
headers = {
"content-type": "application/json",
"X-RapidAPI-Host": "vehicle-damage-assessment.p.rapidapi.com",
"X-RapidAPI-Key": "bd31338435msha00c21a34e0a18dp10bb9cjsn067429eb078f"
}
response = requests.request("POST", url, json=payload, headers=headers)
res = response
json_data = json.loads(response.text)
anoted_url = (json_data['output_url'])
for item in (json_data['output']['elements']):
damage_cat = (item['damage_category'])
damage_loc = (item['damage_location'])
damage_per = (item['score'])
damage_per = damage_per*100
final_list.append(damage_cat)
final_list.append(damage_loc)
final_list.append(damage_per)
image = open("C:\\Users\\286303\\Pictures\\Car Damaged\\Damaged\\052a5e6f08d199f88379964c4e4d6196--car-body-repairs-audi-a.jpg", 'rb')
image_read = image.read()
image_64_encode = base64.b64decode(image_read)
print(image_64_encode)
vehicle_damage(image_64_encode)

you need to change it like this:
def vehicle_damage(img: bytes):
...
payload = {"draw_result": True, "image": str(img)}
...
make 'img' as bytes in function's arguments and make it string when put it in your payload dict.

Related

Extract Json Data with Python

My Jason Data looks like this:
{
"componentId": "SD1:1100047938",
"componentType": "Device",
"name": "WR50MS15-7938 (WR 33)",
"product": "SB 5000TL",
"productTagId": 9037,
"pvPower": 886,
"serial": "1100047938",
"specWhOutToday": 3.0909803921568626,
"specWhOutYesterday": 2.924313725490196,
"state": 307,
"totWhOutToday": 15764,
"totWhOutYesterday": 14914
}
How could i only extract:
"state" to a separate file
"pv-power" to a sperate file ?
Thanks !
import requests
import json
import urllib3
import sys
import requests
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
fehler = '"state": 35'
urltoken = "https://172.16.63.100/api/v1/token"
urldaten = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
urlaktuell = "https://172.16.63.100/api/v1/widgets/gauge/power?componentId=Plant%3A1&type=PvProduction"
data = {
"grant_type": "password",
"username": "...",
"password": "...",
}
response = requests.post(urltoken, data, verify=False)
#print(response.json())
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
with open('/usr/lib/nagios/plugins/check_DataManager/token.txt') as json_file:
data1 = json.load(json_file)
token = data1["access_token"]
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urldaten, headers=headers, data=payload, verify=False)
data = json.loads(response.text)
with open('/usr/lib/nagios/plugins/check_DataManager/info.txt', 'w') as f:
data = json.dump(data, f, indent = 2)
if fehler in open('/usr/lib/nagios/plugins/check_DataManager/info.txt').read():
print("Mindestens ein Wechselrichter hat einen Fehler!")
exit(1)
else:
print("Alle Wechselrichter Online!")
exit(0)
payload={}
headers = {'Authorization': 'Bearer ' + token }
response = requests.request("GET", urlaktuell, headers=headers, data=payload, verify=False)
aktuell = json.loads(response.text)
daten = aktuell["value"]
print("Aktuelle Leistung:", 0.001*daten , "KWh")
I now managed to do all i wanted like this :)
Well at first it ignores all Certificate Warnings i have been getting from my SMA Monitoring Device.
Then it gathers the Bearer Access token an stores it into an .txt file
After this is sends a request for json data to urldaten and urlaktuell. This Data is then stored in info.txt and ertrag.txt :) In this Files it checks if there is a faulty Inverter stored :)
url = "https://172.16.63.100/api/v1/overview/Plant:1/devices?todayDate=2022-10-17T12%3A53%3A16.746Z"
payload={}
headers = {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NjYwMTU0MzMsInN1YiI6Ik1iYWNobCIsInVpZCI6Ijc1YmNkNTM2LTFhOTYtNDQ4My05MjQxLWZkNjY5Zjk3M2Y5OCIsImV4cCI6MTY2NjAxOTAzM30.bMMAsD8iPrAXDp7fbnwYL3Y8lj4Ktok3tU9NHZkYq8s'
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
#print(response.text)
data = json.loads(response.text)
with open('data.json', 'w') as f:
data = json.dump(data, f, indent = 2)
This is my Code for gathering the JSON Data.
I need to exctract the above mentioned Values.
After you run data = json.loads(response.text) , the json is loaded into your data variable as a python dictionary.
So state = data.state and pvPower = data.pvPower should give the info you're after.
I'm not exactly sure what you want to do with that information, as far as extracting to a separate file. But, json.dump() does output data to a json file.

python dynamic value in api payload

Hey i have an api request and i need the value in payload to by dynamic, i tried with f'' but it wont make it dynamic
will appreciate your help.
import requests
url = "https://www.stie.com/api/conversations/KJQ-CZHNR-985/fields"
valuetarget = "123"
payload = {'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
'customfields': '[{"code": "orderno", "value":"valuetarget"}]'}
files = [
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
You just need to load json and dump it to string and send it.
import json
valuetarget = "123"
payload = {
'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
'customfields':
json.dumps([{"code": "orderno", "value":valuetarget}])
}
You should escape the curly braces in the formatted string, like so:
f'[{{"code": "orderno", "value":"{valuetarget}"}}]'
But why not let requests format the string for you?
import requests
url = "https://www.stie.com/api/conversations/KJQ-CZHNR-985/fields"
valuetarget = "123"
# 'customfields' is now a list of dictionaries
payload = {'apikey': 'zbyc88srdi333d3dpq5lye48pgg1tfo1pnjvj65ld',
'customfields': [{"code": "orderno", "value": valuetarget}]}
files = [
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)

Trying to retrieve data from the Anbima API

I'm trying to automate a process in which i have to download some brazilian fund quotes from Anbima (Brazil regulator). I have been able to work around the first steps to retrieve the access token but i don't know how to use the token in order to make requests. Here is the tutorial website https://developers.anbima.com.br/en/como-acessar-nossas-apis/.
I have tried a lot of thing but all i get from the request is 'Could not find a required APP in the request, identified by HEADER client_id.'
If someone could share some light. Thank you in advance.
import requests
import base64
import json
requests.get("https://api.anbima.com.br/feed/fundos/v1/fundos")
ClientID = '2Xy1ey11****'
ClientSecret = 'faStF1Hc****'
codeString = ClientID + ":" + ClientSecret
codeStringBytes = codeString.encode('ascii')
base64CodeBytes = base64.b64encode(codeStringBytes)
base64CodeString = base64CodeBytes.decode('ascii')
url = "https://api.anbima.com.br/oauth/access-token"
headers = {
'content-type': 'application/json'
,'authorization': f'Basic {base64CodeString}'
}
body = {
"grant_type": "client_credentials"
}
r = requests.post(url=url, data=json.dumps(body), headers=headers, allow_redirects=True)
jsonDict = r.json()
##################
urlFundos = "https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/mercado-secundario-TPF"
token = jsonDict['access_token']
headers2 = {
'content-type': 'application/json'
,'authorization': f'Bearer {token}'
}
r2 = requests.get(url=urlFundos, headers=headers2)
r2.status_code
r2.text
I was having the same problem, but today I could advance. I believe you need to adjust some parameters in the header.
Follows the piece of code I developed.
from bs4 import BeautifulSoup
import requests
PRODUCTION_URL = 'https://api.anbima.com.br'
SANDBOX_URL = 'https://api-sandbox.anbima.com.br'
API_URL = '/feed/fundos/v1/fundos/'
CODIGO_FUNDO = '594733'
PRODUCTION = False
if PRODUCTION:
URL = PRODUCTION_URL
else:
URL = SANDBOX_URL
URL = URL + API_URL + CODIGO_FUNDO
HEADER = {'access_token': 'your token',
'client_id' : 'your client ID'}
html = requests.get(URL, headers=HEADER).content
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())
The sandbox API will return a dummy JSON. To access the production API you will need to request access (I'm trying to do this just now).
url = 'https://api.anbima.com.br/oauth/access-token'
http = 'https://api-sandbox.anbima.com.br/feed/precos-indices/v1/titulos-publicos/pu-intradiario'
client_id = "oLRa*******"
client_secret = "6U2nefG*****"
client_credentials = "oLRa*******:6U2nefG*****"
client_credentials = client_credentials.encode('ascii')
senhabytes = base64.b64encode(client_credentials)
senha = base64.b64decode(senhabytes)
print(senhabytes, senha)
body = {
"grant_type": "client_credentials"
}
headers = {
'content-type': 'application/json',
'Authorization': 'Basic b0xSYTJFSUlOMWR*********************'
}
request = requests.post(url, headers=headers, json=body, allow_redirects=True)
informacoes = request.json()
token = informacoes['access_token']
headers2 = {
"content-type": "application/json",
"client_id": f"{client_id}",
"access_token": f"{token}"
}
titulos = requests.get(http, headers=headers2)
titulos = fundos.json()
I used your code as a model, then I've made some changes. I've printed the encode client_id:client_secret and then I've copied and pasted in the headers.
I've changed the data for json.

Kuna Code activation error by API, how to solve?

Good day!
I am trying to use the API of the Kuna.io exchange
There is a method in the documentation: /v3/auth/kuna_codes/redeem
I get the error:
{"messages": ["signature_is_incorrect"]}
Works correctly with other methods
import requests
import time
import hmac
import hashlib
url = "https://api.kuna.io/v3/auth/kuna_codes/redeem"
api_path = "/v3/auth/kuna_codes/redeem"
secret_key = 'key'
public_key = 'key'
nonce = str(int(time.time()*1000.0))
body = str('')
msg = api_path+nonce+body
kun_signature = hmac.new(secret_key.encode('ascii'), msg.encode('ascii'), hashlib.sha384).hexdigest()
payload = {"code": "ZC7Xr-TBcfa-DW3hg-xNUr8-cxnp2-CHada-QT9Yr-L14DZ-5pyjA-UAH-KCode"}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
'kun-nonce': nonce,
'kun-apikey': public_key,
'kun-signature': kun_signature,
}
response = requests.request("PUT", url, json=payload, headers=headers)
print(response.text)
My suspicions are that the method is wrong since the signature works correctly with other methods.
The signature should contain a body. Try with this one
payload = {"code": "ZC7Xr-TBcfa-DW3hg-xNUr8-cxnp2-CHada-QT9Yr-L14DZ-5pyjA-UAH-KCode"}
body = json.dumps(payload)
msg = api_path+nonce+body

Python: how to send html-code in a POST request

I try to send htm-code from python script to Joomla site.
description = "<h1>Header</h1><p>text</p>"
values = {'description' : description.encode(self.encoding),
'id = ' : 5,
}
data = urlencode(values)
binData = data.encode(self.encoding)
headers = { 'User-Agent' : self.userAgent,
'X-Requested-With' : 'XMLHttpRequest'}
req = urllib2.Request(self.addr, binData, headers)
response = urllib2.urlopen(req)
rawreply = response.read()
At Joomla-server I got the same string but without html:
$desc = JRequest::getVar('description', '', 'POST');
What's wrong?
You should use requests
pip install requests
then
import requests
description = "<h1>Header</h1><p>text</p>"
values = dict(description='description',id=5)
response = requests.post(self.addr,data=values)
if response.ok:
print response.json()
or if joomla didnt return json
print response.content
JRequest::getVar or JRequest::getString filter HTML code. But it can be turned off:
$desc = JRequest::getVar('description', '', 'POST', 'string', JREQUEST_ALLOWHTML);

Categories

Resources