Python Post json API without Request - python

I am on a system without pip and its not planned to use.
Put I have do to a post with python to an API (With this post I want to add a row in a postgresdb)
it works with the following code. but there are two columns which are jsonb in postgres.
With this code these columns have quotation marks, so the json doesn't work properly.
How can I avoid this?
import urllib.parse
import urllib.request
def_post_wo_requ
json_post= { 'store' : 'Jack', 'store_detail' : {'Tel1':'00000','Tel2':'11111'}}
url=host+'/'+ pg_table
data = urllib.parse.urlencode(json_post)
data = data.encode('utf-8')
print(data)
The print data shows this:
b'store=Jack&store_detail=%7B%27Tel1%27%3A+%2700000%27%2C+%27Tel2%27%3A+%2711111%27%7D'
and the json in the db is this:
"{'Tel1': '00000', 'Tel2': '11111'}"

Do you need to convert the data to a string?
Would not this ("classic" approach) work ?
from urllib import request
import json
url = 'https://myapp.domain/endpoint'
json_data= {'store': 'Jack', 'store_detail': {'Tel1': '00000','Tel2': '11111'}}
req = request.Request(url, method="POST")
req.add_header('Content-Type', 'application/json')
data = json.dumps(data)
data = data.encode()
r = request.urlopen(req, data=data)

Related

How to Itreate through Json

I need [0] to increase everytime and fetch the data when index change. from 0 to 13
import requests as r
import json
url = "https://services6.arcgis.com/bKYAIlQgwHslVRaK/arcgis/rest/services/CasesByRegion_ViewLayer/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json"
response = urlopen(url)
Data= json.load(response )
for index in Data:
list = Data['features'][0]['attributes']
[0]+1
print(list)
Here is another simple approach without using urllib:
import requests as r
import json
url = "https://jsonplaceholder.typicode.com/todos/1"
response = r.get(url)
data = response.json()
print(data)
requests.get().json() delivers the complete dict from the response payload:
import requests as r
response = r.get(url)
Data = response.json()
Your json.load() doesn't work as expected because response is a dictionary from the requests module, containing some HTTP stuff like status code, reason, encoding. For API calls, this is not what you want (HTTP errors should be handled with exceptions). What you want is response.json() or response.text.
Also, you imported requests but didn't use it? I don't know about urlopen(). Use requests.get().

From-data api create urllib2 in python

I have call so many api with the help of urllib2 json type. But now i want to crate from-data api with the help of urllib2 and it is not working
I have post api and url and this data
Dummy url = https://www.example.com/xyz?id=32323232
dummy data {'data': "here"}
data should be sent by form-data not raw json type
how can we write code in python with urllib2
url = https://www.example.com/xyz?id=32323232
data = {'data': "here"}
header = {'ContentType' : 'multipart/form-data'}
request = urllib2.Request(url, data, header)
response = urllib2.urlopen(request)
Refer : https://www.pythonforbeginners.com/python-on-the-web/how-to-use-urllib2-in-python/

How to send json from Rails

From Python I make request to Rails app, and get this response:
{u'answer': u'ok'}
and in Rails side code like that:
#res = {'answer'=> 'ok'}
render json: #res
May this problem on python side? Here code of python:
import requests
import json
URL1 = 'http://'
PARAMS = {'login': 'asd'}
r = requests.get(url = URL1, params = PARAMS)
data = r.json()
print(data)
How I can get clear json
You can try as data = r.as_json
You are specifically parsing the JSON contained in the response body into Python objects using r.json(). Use r.text to see the textual representation of the response body.
In a Rails controller you can do:
render :json => data.to_json and return
In the CLI you'd do:
puts data.to_json
Make sure to include json or a compatible gem in your CLI code.

Sending metadata along with a dataframe using Requests POST request

I'm using pythons Requests to send a Pandas Dataframe to a Flask server. The dataframe has about 2 million rows and 16 columns. I want to send a config dictionary along with the dataframe, as metadata. At the moment I am able to send the dataframe as a JSON file, however, I can't find any way to attach the metadata in the same post request.
Here's my code:
Client side:
# Post request containing 1. The dataset (pandas df) 2. The metadata (dict)
dataset = dataset.to_json(orient='split')
metadata = {'dataset ID': "makis", 'date start': "1", 'date end': "2"}
url = "http://localhost:8081/upload_dataset"
r = requests.post(url, data=dataset)
return r.text
Server side:
#app.route("/upload_dataset", methods=['POST'])
def upload_dataset():
from werkzeug.datastructures import FileStorage
payload = request.stream
dataset = pd.read_json(payload, typ='frame', orient='split')
FileStorage(payload).save('dataset.csv')
return 'File Uploaded & Standing by', 200
Once serialized to json, your dataset is plain text. To add more parameters from there, your only options are embed your payload along with metadata in post parameters, resulting in url-encoding the json. Or embed your payload in a top-level json post, thus double-encode in json.
You would gain in clarity and maybe performance if you left json encoding job to requests instead. In this way you could add data and still encode/decode only once.
Example
dataset = dataset.to_dict(orient='list')
post_data = {'dataset ID': "makis", 'date start': "1", 'date end': "2", 'payload': dataset}
url = "http://localhost:8081/upload_dataset"
r = requests.post(url, json=post_data)
Server side:
#app.route("/upload_dataset", methods=['POST'])
def upload_dataset():
post_data = request.get_json()
## Use of meta data keys e.g. post_data['date start']
dataset = pd.from_dict(post_data['payload'], orient='columns')

Python web scrape data request error

I'm trying to retrieve the response json data by a web site that I call.
The site is this:
WebSite DriveNow
On this page are shown on map some data. With browser debugger I can see the end point
end point
that sends response data json.
I have use this python to try scrape the json response data:
import requests
import json
headers = {
'Host': 'api2.drive-now.com',
'X-Api-Key': 'adf51226795afbc4e7575ccc124face7'
}
r = requests.get('https://api2.drive-now.com/cities/42756?expand=full', headers=headers)
json_obj = json.loads(r.content)
but I get this error:
hostname doesn't match either of 'activityharvester.com'
How I can retrieve this data?
Thanks
I have tried to call the endpoint that show json response using Postam, and passing into Header only Host and Api-Key. The result is the json that i want. But i i try the same call into python i recive the error hostname doesn't match either of 'activityharvester.com'
I don't understand your script, nor your question. Why two requests and three headers ? Did you mean something like this ?
import requests
import json
headers = {
'User-Agent': 'Mozilla/5.0',
'X-Api-Key':'adf51226795afbc4e7575ccc124face7',
}
res = requests.get('https://api2.drive-now.com/cities/4604?expand=full', headers=headers, allow_redirects=False)
print(res.status_code, res.reason)
json_obj = json.loads(res.content)
print(json_obj)

Categories

Resources