[newbie question]
I'm trying to create a new record in my Airtable base using python 3.
The curl commands in the documentation are as follows:
$ curl -v -XPOST https://api.airtable.com/v0/restoftheurl \
-H "Authorization: Bearer My_API_Key" \
-H "Content-type: application/json" \
-d '{
"fields": {
"Item": "Headphone",
"Quantity": "1",
"Customer_ID": [
"My_API_Key"
]
}
}'
The python code I tried to use is:
import requests
API_URL = "https://api.airtable.com/v0/restoftheurl"
data = {"Authorization": "Bearer My_API_Key","Content-type":
"application/json","fields": {"Item": "randomitem","Quantity":
"5","Customer_ID": ["randomrecord"]}}
r = requests.post(API_URL, data)
print(r.json())
Where the response is the error:
{'error': {'type': 'AUTHENTICATION_REQUIRED', 'message': 'Authentication required'}}
How am I supposed to properly authenticate this, or am I way of?
You need to distinguish the body (data) from the headers. Using the json named argument automatically set the content type to application/json :
import requests
API_URL = "https://api.airtable.com/v0/restoftheurl"
headers = {
"Authorization": "Bearer My_API_Key"
}
data = {
"fields": {
"Item": "randomitem",
"Quantity": "5",
"Customer_ID": ["randomrecord"]
}
}
r = requests.post(API_URL, headers=headers, json=data)
print(r.json())
Related
I have to send commands to an API using Python.
The API's documentation was written to send receive CURL commands.
I converted the curl string
curl -H "Authorization: Bearer accesstoken" https://www.website.com/feed.php?command=COMMAND
Into this:
import requests
headers = {
'Authorization': 'Bearer accesstoken',
}
params = {
'command': "COMMAND",
}
response = requests.get('https://www.website.com/feed.php', params=params, headers=headers)
The problem is if the command I want to send is a complex command like:
curl -H "Authorization: Bearer accesstoken" https://www.website.com/feed.php?command=COMMAND&wake=10
the conversion to Python won't work as this "&wake=10" isn't accepted.
Any ideas on how to circumvent this?
You can pass multiple query parameters in the params dictionary - requests will turn them into the required URL format.
import requests
headers = {
'Authorization': 'Bearer accesstoken',
}
params = {
'command': "COMMAND",
'wake': 10
}
response = requests.get('https://www.website.com/feed.php', params=params, headers=headers)
You need to split on the &, resulting in multiple key + value pairs in params.
params = {
"command": "COMMAND",
"wake": "10"
}
I have this request using a curl command and I want to translate it to python using the requests library
curl -X POST https://endpoint/prod/api/Translations/start \
-H 'Authorization: Bearer <accessToken>' \
-H 'Content-Type: application/json' \
-d '{ "text": ["first segment to translate.", "second segment to translate."], "sourceLanguageCode": "en", "targetLanguageCode": "de", "model": "general", "useCase": "testing"}'
You can use requests library.
The following curl:
curl -X POST "https://www.magical-website.com/api/v2/token/refresh/" \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"refresh": "$REFRESH_TOKEN"
}'
I wrote in python the following way:
import requests
def get_new_token():
url = 'https://www.magical-website.com/api/v2/token/refresh/'
token = constants.THE_TOKEN
payload = f'{{ "refresh": "{token}" }}'
headers = {"accept": "application/json", "Content-Type": "application/json"}
print("Token handling ....")
r = requests.post(url, data=payload, headers=headers)
print(f"Token status: {r.status_code}")
return r.json()['access']
You can try this one.
import requests
url = "https://endpoint/prod/api/Translations/start"
payload = {
"text": ...,
"sourceLanguageCode": ...,
...
}
headers = { "Authorization": "Bearer ...", "Content-Type": "application/json" }
res = requests.post(url, data = payload, headers = headers)
print(res.status_code)
print(res.text)
Is it that my server is not supporting this request or I am sending some wrong data which doesn't seem to be the case.
# importing the requests library
import requests
# defining the api-endpoint
API_ENDPOINT = "http://10.176.14.170:5000/api/du/v1"
# data to be sent to api
data = {"name":"du1"}
# sending post request and saving response as response object
r = requests.post(url = API_ENDPOINT, data = data)
print(r)
Response:
<Response [415]>
Success from a web CLI that is running on same server.
post /api/du/v1
POST api/du/v1 {"name":"du1"}
I also get success when I send the POST request using curl command from same server
While the original curl command was not included, though it was stated that the data was sent via POST to the server as is. I am going to assume that the curl -d '{"name":"du1"}' was used along with the appropriate headers being sent. Now to demonstrate this, the following command is used:
$ curl http://httpbin.org/post -d '{"name":"du1"}' -H 'Content-Type: application/json'
{
"args": {},
"data": "{\"name\": \"du1\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "15",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.0"
},
"json": {
"name": "du1"
},
"url": "http://httpbin.org/post"
}
Note the bolded data key - that's what likely was being read by your application. On the contrary, doing the same thing as done with the provided usage of requests at the same endpoint:
>>> import requests
>>> r = requests.post('http://httpbin.org/post', data={"name":"du1"})
>>> print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"name": "du1"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "8",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": null,
"url": "http://httpbin.org/post"
}
Note how the requests had been decoded into a form simply because a) the data submitted was a raw dictionary, and not a JSON encoded string and b) the header was not supplied. To fix this, simply encode the data and supply the appropriate headers:
>>> import requests
>>> import json
>>> data = json.dumps({"name":"du1"})
>>> r = requests.post(
... 'http://httpbin.org/post', data=data,
... headers={'Content-Type': 'application/json'})
>>> print(r.text)
{
"args": {},
"data": "{\"name\": \"du1\"}",
"files": {},
"form": {},
...
"json": {
"name": "du1"
},
...
}
Note that the server is now able to interpret the provided data, and can decode that as JSON.
I have a cURL command that I would like to port to Python.
curl -XGET "http://localhost:9200/nuix-7674bc4a60b74ea7bac8996a98b0cb94;item;schema-version=1/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"regexp": {
"content": "(p)hotos"
}
}
}'
It successfully returns a non-error response.
Here is what I tried in Python 3.6 using the requests package.
import requests
import json
# api-endpoint
url = "http://localhost:9200/nuix-7674bc4a60b74ea7bac8996a98b0cb94;item;schema-version=1/_search"
# headers
headers = {'Content-type': 'application/json'}
# Define JSON String
params = """
{
"query": {
"regexp":{
"content": "(p)hotos"
}
}
}
"""
params = json.loads(params)
print(params)
# sending get request and saving the response as response object
response = requests.get(url=url, params=params, headers=headers)
# extracting data in json format
data = response.json()
print(data['hits']['total'])
print('DONE')
The response response._content states this error:
b'{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"request [/nuix-7674bc4a60b74ea7bac8996a98b0cb94;item;schema-version=1/_search] contains unrecognized parameter: [query]"}],"type":"illegal_argument_exception","reason":"request [/nuix-7674bc4a60b74ea7bac8996a98b0cb94;item;schema-version=1/_search] contains unrecognized parameter: [query]"},"status":400}'
What is the correct way to set params?
requests expects a dict as params not a string. I'm not sure this is your problem but you could try rewriting as:
params = {
"query": {
"regexp":{
"content": "(p)hotos"
}
}
}
Check out this section from the docs for a walkthrough: http://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls
The solution is to use the json parameter, rather than params.
response = requests.get(url=url, json=params, headers=headers)
I probably could have rewritten the JSON string to a dictionary, but I am using a tool -- Kibana that automatically generates a cURL payload. This solution allows me to copy and paste the JSON payload into my Python script.
I'm porting some automation scripts from bash to python, and they're almost ll curl commands of the following format:
curl -k -H "Content-Type: application/json" -X POST -d '{ "Request": {
"MessageID": "xxxx",
"MessageDateTime": "xxxxx",
"SourceSystem": "xxxx",
}
}' https://myUrl.xxx
What's the best way to structure this in Python accurately? So far I have:
import requests
headers = {'Content-Type': 'application/json'}
payload = {'All the data'}
conn = httplib.HTTPConnection("myUrl.xxx")
conn.request("POST", "", payload, headers)
response = conn.getresponse()
print response
I want to make sure the -k, -d, and -x bash options are being reflected in this script. Thank you!
You can use requests.post directly. -k corresponds to verify=False:
from datetime import datetime as DateTime
import requests
import json
URL = "https://myUrl.xxx"
message = {
"Request": {
"MessageID": "xxxx",
"MessageDateTime": DateTime.now().isoformat(),
"SourceSystem": "xxxx",
}
}
response = requests.post(URL, data=json.dumps(message), verify=False, headers={"Content-Type":"application/json"})
data = response.json()