Error in POST API python while trying to send data - python

Here is the json data to send
{
"mobile": "1234567890"
}
Here is the python code:
from urllib import response
import requests
import urllib
import json
query = {"mobile": "1234567890"}
headers = {"Content-Type":"application/json"}
url = 'https://cdn-api.co-vin.in/api/v2/auth/public/generateOTP'
y = requests.post(url, data=query, headers=headers)
print(y)
Response from the API:
c:/pythonprojects/covid-otp-requests.py
<Response [400]>
I am a newbie, I don't understand the mistake I am making, Can someone please help me out?.

Fix here:
from urllib import response
import requests
import urllib
import json
params = {
"mobile": "1234567890"
}
headers = {"Content-Type":"application/json"}
response = requests.post("https://cdn-api.co-vin.in/api/v2/auth/public/generateOTP", data=json.dumps(params))
print(response)
details = response.json()
print(details)
The parameters has to be sent in json format which I making a mistake in.
Thank you all!

Related

In python, how can we connect to API using a certificate, subscription key?

I am trying to connect to an api for which I was provided with link, certificate(.p12) and subscription key.
Having some issue while giving the certificate details. I am trying in the following 2 ways:
1.
import json
from requests_pkcs12 import get,post
url = 'https://....'
pkcs12_filename = '<certificate file path>'
pkcs12_password = '<certificate password>'
headers = {
# Request headers
'Subscription-Key': '<subscription key>',}
response = post(url, headers=headers, verify=False, pkcs12_filename=pkcs12_filename,pkcs12_password=pkcs12_password)
print(response.status_code)
import http.client, urllib.request, urllib.parse, urllib.error, base64
#file = "<certificate path>"
headers = {
'Subscription-Key': '<subscriptionkey>',
#'cert' : crypto.load_pkcs12(open(file, 'rb').read(), "<password>").get_certificate(),
'file' : '<certificate path>',
'password':'<certificate password>',}
params = urllib.parse.urlencode({
})
conn = http.client.HTTPSConnection('<api main link>')
conn.request("GET", "<api remaining link>" , params, headers)
response = conn.getresponse()
data = response.read()
print("Status: {} and reason: {}".format(response.status, response.reason))
conn.close()
I am new to API concept. Will someone help me over this?
Refered to this link: How to use .p12 certificate to authenticate rest api
But didn't get what i need to put in data variable

Python API Calling Random.org

So I am new to APIs but I'm trying to get more practice with them. I am getting connection okay (200) but whenever I try to print the results I receive this error:
{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error","data":null},"id":null}
Here's my code, with the api key redacted:
url = 'https://api.random.org/json-rpc/1/invoke'
data = {'jsonrpc':'2.0','method':'generateIntegers','params': {'apiKey':'mykeygoeshere','n':10,'min':1,'max':10,'replacement':'true','base':10},'id':24565}
response = requests.get(url,data)
print(response.text)
Any help is appreciated
import requests
import json
url = 'https://api.random.org/json-rpc/1/invoke'
data = {'jsonrpc':'2.0','method':'generateIntegers','params': {'apiKey':'mykey','n':10,'min':1,'max':10,'replacement':'true','base':10},'id':24565}
params = json.dumps(data)
response = requests.post(url,params)
print(response.text)
This Code Works in Version 2
import requests
import json
raw_data = {
"jsonrpc": "2.0",
"method": "generateIntegers",
"params": {
"apiKey": "your-api-key",
"n": 6,
"min": 1,
"max": 6,
"replacement": True
},
'id':1
}
headers = {'Content-type': 'application/json','Content-Length': '200', 'Accept': 'application/json'}
data=json.dumps(raw_data)
response = requests.post(
url='https://api.random.org/json-rpc/2/invoke',
data=data,
headers=headers
)
print(response.text)
So for random.org you have to make a post request to the url
https://api.random.org/json-rpc/2/invoke
with data which is stored in the variable raw_data but first you should convert the dictionary object to json format this can be done using the line
data=json.dumps(raw_data)
and headers variable stores the format of the POST request
The request is done using the requests module post method
and then we get response in json format which is converted to printable format by
print(response.text)
be sure to store your api_key in the raw_data variable

how to pass value from json-rpc and let python read it

I added {"value":10} in json-rpc:
import requests
import xbmc
import xbmcgui
import xbmcplugin
import xbmcaddon
import json
headers = {
'Content-Type': 'application/json',
}
data = '{"jsonrpc":"2.0","method":"Addons.ExecuteAddon","params":
{"addonid":"script.activatewindowid"},"params":{"value": 10},"id":1}'
response = requests.post('http://192.168.1.200:8080/jsonrpc',
headers=headers,
data=data,
auth=('user', '1234'))
And I want the python script can understand the value:10 and use it in his IF function:
import json
json_string = what should be here???
parsed_data = json.loads(json_string)
x = parsed_data["value"]
if x == 10:
xbmc.executebuiltin('ActivateWindow(9000)')
else:
xbmc.executebuiltin('ActivateWindow(1199)')
the json-rpc data reached coerrectly to python script but the python didn't understand how to deal with x value?
Any suggestion to solve this issue please?

Firebase JWT does not work with Python and UID JWT

Why?
import requests
import json
from firebase_token_generator import create_token
auth_payload = { "uid": "BAD-BAD-BAD-KEY"}
CREDENTIAL = create_token("my-secret-key", auth_payload)
payload = json.dumps({"dude":"me"})
path = "/test1/test.json"
url = "https://omgfirebase.firebaseio.com%s?auth=%s" % (path,CREDENTIAL)
print url
print payload
r = requests.put(url, data=payload)
print r.status_code
print r.text
{"dude": "me"}
200
If I use a bad invalid uid, I can still write to the database. Why? The data is in Firebase Database.

Issue with making transaction in Neo4J Python

I am trying to send a POST request with a Neo4j transaction query. Although I get a response 200 the node is not created. This is my Python script:
import requests
import json
import csv
headers = {'content-type': 'application/json'}
url = "http://localhost:7474/db/data/transaction/commit"
checkNode = {"query" : '{"statements": [{"statement":"CREATE (n:test) RETURN n"}]}'}
mkr =requests.post(url, data=json.dumps(checkNode), headers=headers)
print(mkr)
I haven't used transactions before and nver tried to create one through the Rest Api. What am I doing wrong here?
It seems unlikely to me that you're receiving a response code of 200; you should be getting a 500 as the transactional endpoint doesn't accept a query parameter. Try this:
import requests
import json
import csv
headers = {'content-type': 'application/json'}
url = "http://localhost:7474/db/data/transaction/commit"
checkNode = {"statements":[{"statement":"CREATE n RETURN n"}]}
mkr = requests.post(url, data=json.dumps(checkNode), headers=headers)
print(mkr.text)

Categories

Resources