How to load json url and get data using python - python

I want to load this api url and fetch data. I am using this code to get this but i am getting 400 bad request.
Actual website link is (https://www.lffs.eu/les-clubs/)
'''
import urllib, json
enter code hereurl = "https://gestion.lffs.eu/lms_league_ws/public/api/v1/club/byMyLeague?filter=&club_status_id=1&page=2&pagination=21"
response = urllib.urlopen(url)
data = json.loads(response.read())
print(data)
'''

There is a problem with the way you used the API. If you run it on Google you will see the error. The API expects some kind of token from you. Make sure you provide it first.

Related

python convert from request python to send api in json

guys I want to create a python API and request msg and get a response with JSON and send it to knime workflow and work with it
this is my script in python the fact is that he give me json.decoder.jsondecodeError
from importlib.metadata import files
import requests
url ='https://api.edination.com/v2/edifact/read'
headers = {'Ocp-Apim-Subscription-Key': '3ecf6b1c5cf34bd797a5f4c57951a1cf'}
files = {'file':open('C:\\Users\\hcharafeddine\\Desktop\\EDI\\Interchange_1654767219416.edi','rb')}
r = requests.post(url,files=files)
r.json()
We'll need more info to help further. I understand if you don't want to share the content of the EDI message, so here are a few things to try:
The EDINation website allows you to paste an EDI message in and it'll show you the JSON output that the API will return
It also has a sample EDIFACT document you can select and then save locally to run through your python script - you can then share the results here
You can also use HTTPToolkit to inspect the API request and response to troubleshoot further.

Thoughtspot: API calls to fetch metadata via Python

I'm trying to fetch metadata from thoughtspot. I am able to call the url using browser and fetch the data. But here I'm trying to achieve it via python program. According to thougthspot documentation. I have to enable trusted authentication and pass my secret key & username to obtain a token which I can use in my program.
https://developers.thoughtspot.com/docs/?pageid=api-auth-session
my username : username#username.com
secret key : secret-key
Below is my code:(generated by postman)
import requests
url = "https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/session/auth/token?auth_token=secret-key&access_level=FULL&username=username#username.com"
payload={}
headers = {}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
I'm getting Bad request error. Anyone here using thoughtspot over this issue. Appreciate your support very much.
Error I'm getting:
{"type":"Bad Request","description":"The server could not understand the request due to invalid syntax."}
I can fetch data by calling the api using a web-browser. Below url returns list of all meta-data objects. I want to achieve this using a python program (I have to authenticate first & call the below URL - Authentication step is not working for me when I tried to follow the documentation)
https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/metadata/list
Did you try changing the url so that it includes the domain name?
Also post the error you are getting. And a screenshot of a working request would be great!

Python: get json data from https url using sockets

I need to get some json data using API
import requests
url = 'https://example.com/api/some-info/'
response = requests.get(url)
print(response.text) # Here is JSON needeed
And everything fine, except I need to make such requests very often, and API provider says:
You'll be banned if you make more than 5 requests per second, so use
sockets
So, how can I make this work via sockets?
Big thx for advices.

indexing data to solr update url from python code in json format does not update the record in solr

i am using a python function to index data to solr on localhost my code is below
import urllib
import urllib2
def indexSolrVenue():
url = 'http://localhost:8983/solr/venue/update/json?commit=true&wt=json'
data = '[{"id":"3","title":"allen"}]'
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
in solr i have a schema defined which accepts id and title. i tried using postman chrome extension to send data to the url
http://localhost:8983/solr/venue/update/json?commit=true
with data
[{'id':'2','title':'test1'}]
and it gets indexed properly and is reflecting in solr. but if i run the python code on terminal i get the following output
{"responseHeader":{"status":0,"QTime":6}}
which actually means that the data is getting indexed but on the solr when i try to search it with : query
http://localhost:8983/solr/venue/select?q=*%3A*&wt=json&indent=true
it does not show me the id:3 record.
am i doing something wrong in my code? please help
thanx in advance
req.add_header('Content-type', 'application/json')
You need to set the header for the content in you req object. Please use the above statement just before calling the urllib2.urlopen(req);

Put json data into url with python

I am trying to encode a json parameter within a url for use with the mongolab restAPI.
My url looks something like this
url = 'https://api.mongolab.com/api/1/databases/db/collections/coll?q={"q": "10024"}&apiKey=mykey
I am trying to open it using
urllib2.urlopen(url)
but I run into errors saying that my apikey is incorrect. I know this isn't true because if I copy and paste the url into my browser I get a correct response. I also know that I can access the rest api as long as I don't have the query there (so it must be a json/formatting problem).
So does anyone know how I could encode the json query
{"q": "10024"}
into the url? Thanks!
You'll have to properly URL-encode the string. Use the urllib.quote_plus() function:
url = 'https://api.mongolab.com/api/1/databases/db/collections/coll?q={q}&apiKey={key}'
query = urllib.quote_plus('{"q": "10024"}')
urllib2.urlopen(url.format(q=query, key=your_api_key))
You could also use the requests library.
Example:
import requests
payload = {'q': '10024', 'apiKey': 'mykey'}
r = requests.get("https://api.mongolab.com/api/1/databases/db/collections/coll", params=payload)
print(r.url)
Output:
https://api.mongolab.com/api/1/databases/db/collections/coll?q=10024&apiKey=mykey

Categories

Resources