Convert curl send file to python request - python

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

Related

Python requests.post, cant use a file as "data" variable

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 to properly format data retrieved through GraphQL API into a data frame?

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}.")

Preserving sort order of .json file that gets created from api response

I am having a problem with getting the correct sort order of a .json file that gets created from an api response using PyCharm Community Edition with python 3.7.
This is the api request:
import requests
import json
url = "https://pokemon-go1.p.rapidapi.com/pokemon_names.json"
headers = {
'x-rapidapi-key': "c061ae2dffmshc2a33d10b00cee7p121f42jsn11f39d53dd1e",
'x-rapidapi-host': "pokemon-go1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
If i now print(response.text), I get the following output: (That's how I want my .json file to look like later)
{
"1": {
"id": 1,
"name": "Bulbasaur"
},
"2": {
"id": 2,
"name": "Ivysaur"
},
"3": {
"id": 3,
"name": "Venusaur"
},
"4": {
"id": 4,
"name": "Charmander"
},
"5": {
"id": 5,
"name": "Charmeleon"
}
}
After that I write the response to the file "pokemondata.json" by doing this:
response_json = json.loads(response.text)
writeFile = open("pokemondata.json", "w")
writeFile.write(json.dumps(response_json, indent=4, sort_keys=True))
writeFile.close()
And then the file looks like this:
{
"1": {
"id": 1,
"name": "Bulbasaur"
},
"10": {
"id": 10,
"name": "Caterpie"
},
"100": {
"id": 100,
"name": "Voltorb"
},
"101": {
"id": 101,
"name": "Electrode"
},
"102": {
"id": 102,
"name": "Exeggcute"
}
}
I could not figure out how to get it done so that the file is sorted by ids (or the numbers before the id, e.g. "1") correctly. Could anybody please explain to me, how I can fix it?
import requests
import json
url = "https://pokemon-go1.p.rapidapi.com/pokemon_names.json"
headers = {
'x-rapidapi-key': "c061ae2dffmshc2a33d10b00cee7p121f42jsn11f39d53dd1e",
'x-rapidapi-host': "pokemon-go1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
response_json = response.json()
int_keys = {int(k):v for k,v in response_json.items()}
with open("sample.json", 'w') as file_obj:
json.dump(int_keys, file_obj, indent=4, sort_keys=True)
Issue is with keys in your json which are in string format. Convert them to integers and save in the file

Can't get discord webhook to send with json

I've been trying to make a discord webhook, and for some reason I can't get it to send to save my life. I have all the embeds in a json file, and am trying to run it with python. Any help would be greatly appreciated.
json file:
{
"embeds": [
{
"title": "Title",
"color": 11393254,
"fields": [
{
"name": "Name1",
"value": "value1"
},
{
"name": "Name2",
"value": "value2",
"inline": true
},
],
"footer": {
"text": "Footer",
"icon_url": "random image url"
},
"timestamp": "now",
"thumbnail": {
"url": "random image url"
}
}
],
"username": "Username"
}
python code:
import requests, json
with open('webhook.json') as json_file:
data = json.load(json_file)
url = 'https://discord.com/api/webhooks/xxxxx'
headers={"Content-Type": "application/json"}
requests.post(url, data=json.dumps(data), headers=headers)
What's the status code you get back from the POST? That could be a clue where it's going awry.

Creating a JSON post request with python

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)

Categories

Resources