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);
Related
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.
I want to translate this curl command to python and I am having issue to write form properly:
"https://gitlab.example.com/api/v4/projects/1/variables/NEW_VARIABLE" --form "value=updated value"
I tried :
files = {'file': ('somename.csv', 'value=updated value')}
and
files = {}
files['value'] = 'updated value'
and
files = dict(value=value_to_update)
The whole example:
url = 'https://gitlab.com/api/v4/projects/1/variables/bla?filter[environment_scope]=production'
headers = {
'content-type': 'application/json',
'PRIVATE-TOKEN': '<token>'
}
files = dict(value=value_to_update)
x = requests.put(url, headers=headers, files=files)
Solved: This is how whole request in python using requests should look like
```
headers = {
'PRIVATE-TOKEN': '<token>',
}
files = {
'value': (None, 'update value'),
}
response = requests.put('https://gitlab.com/api/v4/projects/1/variables/variableKey', headers=headers, files=files)
this is the conversion to python requests:
import requests
files = {
'value': (None, 'updated value'),
}
response = requests.post('https://gitlab.example.com/api/v4/projects/1/variables/NEW_VARIABLE', files=files)
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.
I have a Python 2.7 script that looks like this:
import requests
url = "https://example.net/rest/v1/m/4513452615415"
querystring = {"client":"wzas"}
payload = "{\r\n \"sss\" : \""+msse+"\",\r\n\"VisitorId\":\""+mcid+"\",\r\n \"thirdPartyId\": \""+tracking_id+"\",\r\n \"contentAsJson\": \"true\",\r\n \"mbssParameters\": \r\n { \r\n \"mboxMCGLH\": \"6\" \r\n }\r\n}\r\n"
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "289f645d-1543-e6df-87fb-1cef88f110c5"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
return (response.text)
The output is sent to a CSV file and looks like this:
{"thirdPartyId":"559yZDIIs3XvFpLPhvjexK7/jYlT7ZwJXBpNc/ZS4A1saWdodG5pbmdzZWVkcw==","marketingId":"89137879111811717593914206190290951066814","edgeHost":"mrdge21.example.net","content":{"tnsVal":"931113:4:0","contentName":"examplecontent","revenue":14},"sessionId":"4513452615415"}
How would I only output the contentName and tnsVal ?
Thanks in advance
Nick
Since it's pretty clear you're getting a JSON back, try the following line:
import json
response_json = json.loads(response.text)
print(response_json['content']['contentName'], response_json['content']['tnsVal'])
You can try:
>>> import json
>>> data = json.loads(response.text)
>>> data["content"]["tnsVal"]
'931113:4:0'
>>> data["content"]["contentName"]
'examplecontent'
I have this code to read Mashape.com API in python 2. how can i read it in python 3?
code
import urllib, urllib2, json
from pprint import pprint
URL = "https://getsentiment.p.mashape.com/"
text = "The food was great, but the service was slow."
params = {'text': text, 'domain': 'retail', 'terms': 1, 'categories': 1,'sentiment': 1, 'annotate': 1}
headers = {'X-Mashape-Key': YOUR_MASHAPE_KEY}
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(URL, urllib.urlencode(params), headers=headers)
response = opener.open(request)
opener.close()
data = json.loads(response.read())
pprint(data)
i tried this code but it had following error :
import urllib.parse
import urllib.request
URL = "https://getsentiment.p.mashape.com/"
text = "The food was great, but the service was slow."
params = {'text': text, 'domain': 'retail', 'terms': 1, 'categories': 1, 'sentiment': 1, 'annotate': 1}
headers = {'X-Mashape-Key': YOUR_MASHAPE_KEY}
opener = urllib.request.build_opener(urllib.request.HTTPHandler)
request = urllib.request.Request(URL, urllib.parse.urlencode(params), headers)
response = opener.open(request)
opener.close()
data = json.loads(response.read())
print(data)
error :
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
In this line:
request = urllib.request.Request(URL, urllib.parse.urlencode(params), headers)
Try to replace to
data = urllib.parse.urlencode(params).encode('utf-8')
request = urllib.request.Request(URL, data, headers)