I'm trying to create an object through back4app for class sound
the issue which am having that am unable to upload the file.
code used:
import requests
headers = {
"X-Parse-Application-Id": "hidden",
"X-Parse-REST-API-Key": "hidden",
}
data = {
"audio": {
"__type": open("a.mp3", 'rb'),
"name": "a.mp3"
},
"displayText": "test"
}
def main(url):
with requests.Session() as req:
req.headers.update(headers)
r = req.post(url, data=data)
print(r.text)
main("https://parseapi.back4app.com/classes/sounds")
Output:
{"code":111,"error":"schema mismatch for sounds.audio; expected File but got Array"}
You first need to upload the file:
import json,httplib
connection = httplib.HTTPSConnection('parseapi.back4app.com', 443)
connection.connect()
connection.request('POST', '/files/a.mp3', open('a.mp3', 'rb').read(), {
"X-Parse-Application-Id": "${APPLICATION_ID}",
"X-Parse-REST-API-Key": "${REST_API_KEY}",
"Content-Type": "audio/mpeg"
})
result = json.loads(connection.getresponse().read())
Then create the object:
connection.request('POST', '/classes/sounds', json.dumps({
"displayText": "test",
"audio": {
"name": result["name"],
"url:": result["url"],
"__type": "File"
}
}), {
"X-Parse-Application-Id": "${APPLICATION_ID}",
"X-Parse-REST-API-Key": "${REST_API_KEY}",
"Content-Type": "application/json"
})
connection.getresponse().read()
Related
For anonfiles, they only give the curl function to upload a file,
curl -F "file=#test.txt" https://api.anonfiles.com/upload
and I want to use python requests for this.
I tried
import requests
data = requests.put(url = "https://api.anonfiles.com/upload",data=open('file.txt','r').read() )
print(data.text)
Try:
import requests
with open('test.txt', 'rb') as fp:
r = requests.post('https://api.anonfiles.com/upload', files={'file': fp})
print(r.json())
Output:
{
"status": true,
"data": {
"file": {
"url": {
"full": "https://anonfiles.com/oa28JcS2ya/test_txt",
"short": "https://anonfiles.com/oa28JcS2ya"
},
"metadata": {
"id": "oa28JcS2ya",
"name": "test.txt",
"size": {
"bytes": 580,
"readable": "580 B"
}
}
}
}
}
More information here: POST a Multipart-Encoded File
headers = {
"User-Agent": "Mozilla/5.0",
'accept': 'application/json',
'Content-Type': 'application/json',
}
with open("jsonattempt.txt","r") as f:
data = f.read()
json_data = "'" + data + "'"
response = requests.post('https://www.pathofexile.com/api/trade/search/Standard', headers=headers, data=json_data)
print(response)
Generally, there is a curl request like this:
curl -X 'POST' \
'https://www.pathofexile.com/api/trade/search/Standard' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"query": {
"status": {
"option": "online"
},
"type": "Turquoise Amulet",
"stats": [
{
"type": "and",
"filters": [
{
"id": "pseudo.pseudo_total_mana",
"value": {
"min": 47,
"max": 49
},
"disabled": false
}
]
}
]
},
"sort": {
"price": "asc"
}
}'
Which returns a bunch of unnecessary things.
My json_data variable and jsonattempt.txt is the same as -d parameter, I add ' ' to start and to end:
{
"query": {
"status": {
"option": "online"
},
"type": "Turquoise Amulet",
"stats": [
{
"type": "and",
"filters": [
{
"id": "pseudo.pseudo_total_mana",
"value": {
"min": 47,
"max": 49
},
"disabled": false
}
]
}
]
},
"sort": {
"price": "asc"
}
}
I convert curl request to python which is the code on the top, I add the data as json_data and yeet the post request but keep getting 400 Bad Request.
Request 401 is Unauthorized AFAIK, so I dont need an OAuth2 key for this. How can I insert my json file appropiately to the request?
(Same exact json works on https://app.swaggerhub.com/apis-docs/Chuanhsing/poe/1.0.0#/Trade/get_api_trade_fetch__items_ I am just asking how to insert my json file to requests.post correctly.)
Why are you adding quotes around the JSON content? That doesn't make any sense. Those quotes aren't part of your curl request. If you just write...
import requests
headers = {
"User-Agent": "Mozilla/5.0",
"accept": "application/json",
"Content-Type": "application/json",
}
with open("jsonattempt.txt", "r") as f:
data = f.read()
response = requests.post(
"https://www.pathofexile.com/api/trade/search/Standard",
headers=headers,
data=data,
)
print(response)
...it works as expected.
And in fact you can further simplify that; you don't need to read in the file data yourself, you can let requests do that:
import requests
headers = {
"User-Agent": "Mozilla/5.0",
"accept": "application/json",
"Content-Type": "application/json",
}
with open("jsonattempt.txt", "r") as f:
response = requests.post(
"https://www.pathofexile.com/api/trade/search/Standard",
headers=headers,
data=f,
)
print(response)
import json
# some codes here
with open("jsonattempt.txt","r") as f:
data = f.read()
json_data = json.loads(data)
# rest of codes here
Your request requires json type data, while you are passing a string. The json.loads method converts string to json. Try it out.
How can I update my block with Notion? I've tried to follow the instructions on how to update the API but failed. Even when using requests.patch, the response text doesn't change.
Here's the code:
import json, requests
import dotenv, os
def main():
dotenv.load_dotenv(dotenv.find_dotenv())
TOKEN = os.environ.get("TOKEN")
BLOCK_ID = os.environ.get("BLOCK_ID")
headers = {
"Authorization": "Bearer " + TOKEN,
"Notion-Version": "2021-08-16"
}
new_body = {
"paragraph": {
"text": [{
"text": {
"content": "bye bye"
}
}]
}
}
readurl = f"https://api.notion.com/v1/blocks/{BLOCK_ID}"
# res = requests.request("PATCH", readurl, headers=headers, data=json.dumps(new_body))
res = requests.patch(readurl, headers=headers, data=new_body)
print(res.status_code)
print(json.dumps(json.loads(res.text), indent=2))
if __name__ == "__main__":
main()
The status code and text are:
200
{
# other properties... then,
"paragraph": {
"text": [
{
"type": "text",
"text": {
"content": "hello there!",
"link": null
},
"annotations": {
"bold": true,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "hello there!",
"href": null
}
]
}
}
cause u miss Content_Type on header
try change to
headers = {
"Authorization": "Bearer " + TOKEN,
"Notion-Version": "2021-08-16"
"Content-Type": "application/json"
}
the data value will ignore when no content-Type on header, so the 200 respond will show as no data value provide for change.
This is a follow-up to a previous question.
Following this tutorial, I'm trying to get data from a GraphQL API and format the result into a data frame.
Here is my Python script (URL is obfuscated):
import requests
import json
import pandas as pd
url = 'https://mygraphqlserver.com/graphql/'
query = """{ resources { edges { node { id creatorPerson { firstName }} } } }"""
r = requests.post(url, json={'query': query})
json_data = json.loads(r.text)
resources_dict = json_data['data']['resources']['edges']
resources_output = pd.DataFrame(resources_dict)
print(resources_output)
Here is r.text:
{
"data": {
"resources": {
"edges": [
{
"node": {
"id": "file/pt0u8h901qni3d",
"creatorPerson": {
"firstName": "Jérémy"
}
}
},
{
"node": {
"id": "file/f218c8wn4onncj",
"creatorPerson": {
"firstName": "Jérémy"
}
}
},
{
"node": {
"id": "file/i1y7pjk7a6xy2d",
"creatorPerson": {
"firstName": "Jérémy"
}
}
}
]
}
}
}
Here is the ouput of print(resources_output) in the terminal:
node
0 {'id': 'file/pt0u8h901qni3d', 'creatorPerson':...
1 {'id': 'file/f218c8wn4onncj', 'creatorPerson':...
2 {'id': 'file/i1y7pjk7a6xy2d', 'creatorPerson':......
As you can see, I don't succeed to get a proper data frame as I expected (as shown in the tutorial).
Any idea how I can I improve my script to get a properly formatted output?
import requests import json import os
user = os.getenv("USERNAME")
key = os.getenv("KEY")
pwd = os.getenv("PASSWORD")
endpoint = f"https://pkg-graphql-api.*****.com/{user}"
headers = {"Authorization": f"Basic {key}"}
query = {
IPublications(abstract: "metabolics") {
ICitedBy(filterByYear_lte: 2020) {
isbn
count
pkgId
normalizedTitle
IPublishedInConferenceInstanceIds
}
}
}
r = requests.post(endpoint, json={"query": query}, headers=headers)
if r.status_code == 200:
print(json.dumps(r.json(), indent=2))
else:
raise Exception(f"Query failed to run with a {r.status_code}{r.text}.")
I am experimenting with the Rapaport Technet API, and want to hit an endpoint which expects the following JSON:
{
"request": {
"header": {
"username": "my_username",
"password": "my_password"
},
"body": {}
}
}
Code:
url = 'https://technet.rapaport.com:449/HTTP/JSON/Prices/GetPriceChanges.aspx'
headers = {'username': 'my_username', 'password': 'my_password'}
r = requests.post(url, headers)
I get this response:
{
"response": {
"header": {
"error_code": 1001,
"error_message": "Invalid format"
},
"body": {}
}
}
Any idea what the problem could be?
According to this example from Rapaport Technet API docs, that whole JSON is sent to be as data in the POST request. So simply do the same as given here in Requests docs.
json_data = {
"request": {
"header": {
"username": "my_username",
"password": "my_password"
},
"body": {}
}
}
r = requests.post(url, json=json_data)