Reputation: 1252
I have difficulties converting a post request using htppie to python requests.post. This question is in general about how to do such a conversion, but I will use the specific request I was doing as an example.
So I have the following post request using httpie, which works fine:
http post https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet query='{ indexers {
id
}
}'
However, when trying to send the same request using pythons requests library, I tried the following:
import requests
url = 'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet'
query = """'{ indexers {
id
}
}'"""
print(requests.post(url, data = query).text)
This gives a server error (I tried many versions of this, as well as sending a dictionary for the data variable, and they all give the same error). The server error was GraphQL server error (client error): expected value at line 1 column 1
.
Regardless of what the server error is (which is probably server specific), this at least means that the two requests obviously are not exactly the same.
So how would I go about converting this httpie request to using pythons requests library (or any other python library for that matter)?
Upvotes: 2
Views: 1483
Reputation: 436
To pass request payload, you need pass json as string (I used json.dumps()
for conversion). To pass body as form-data, pass just dict.
import json
import requests
url = 'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-mainnet'
payload=json.dumps({'query': '{indexers {id}}'})
headers = {
'Content-Type': 'application/json',
}
response = requests.post(url, headers=headers, data=payload)
Note. I recommend that you try to make this request in Postman and then you can convert the code to Python right in Postman.
Upvotes: 3