On ably.io they have an example where one can use the following curl request to publish a message to a channel:
curl -X POST https://rest.ably.io/channels/channelname/messages \
-u "some_AP.aYYMcQ:VmGHauKOqo-35Zxo" \
-H "Content-Type: application/json" \
--data '{ "name": "greeting", "data": "example" }'
The value passed to -u is an API key that has publish privileges. How does one make the same post request using Python requests library? I searched the documentation but could not find it. Note there is no password here, only the api key.
Thanks in advance.
You could use this:
requests.post("https://rest.ably.io/channels/channelname/messages",
auth=('some_AP.aYYMcQ', 'VmGHauKOqo-35Zxo'), # Equivalent of -u
json={ "name": "greeting", "data": "example" }) # Equivalent of --data
When you use the json option, -H is automatically set to Content-Type: application/json.
Related
I have a flask api that automatically validates and generates Swagger documentation thanks to marshmallow package.
One of the API methods is a POST request that wants a specific id as a string and a list_of_ids as an array of strings. The GUI looks like this:
Whatever I insert, I always get the same reply:
So my question is how do you input such list (or array) or strings? Is there an equivalent curl for it?
Hey I created some example POST request similar to what you reqested in Postman and here is the result cURL:
curl --location --request POST 'http://localhost:5000/api/v1/add_articles_to_article_list' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": "42",
"list_of_ids": [
1,
2
]
}'
I am trying to send a POST/PUT requests with CMD (command prompt in windows 10) using these commands:
1st:
curl -iX PUT -H "Content-Type: application/json" -d \"{\"name\": \"test number 1\", \"description\": \"a website test \", \"category\": \"test\", \"release_date\": \"2017-10-08T01:01:00.776594Z\"}\" localhost:8000/home/search/3
2nd:
curl -iX PUT -H "Content-Type: application/json" -d "{"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}" localhost:8000/home/search/3
and using these commands I am getting a curl: could not resolve error, and when its done naming everything it couldn't resolve I get a server error status 500 I don't understand why its not working cause I tried these commands and they seemed to work before and they don't.
(I thought I had an error in my code and I tried the http request instead of curl and it worked fine with no problem getting the 200 OK status.).
If NOT on Windows, the answer above by #Necklondon with the single quotes should work.
If on Windows, however, you'll need to escape the double quotes within the JSON string.
curl -iX PUT -H "Content-Type: application/json" -d "{""name"": ""test number 1"",""description"": ""A website test"",""category"": ""test"",""release_date"": ""2017-10-08T01:01:00.776594Z""}" http://localhost:8000/home/search/3
In addition, to illustrate how much simpler the CURL command becomes when using a separate file for your JSON...
C:\Users\...\Desktop>TYPE somefile.json
{
"name": "test number 1",
"description": "A website test",
"category": "test",
"release_date": "2017-10-08T01:01:00.776594Z"
}
C:\Users\...\Desktop>curl -iX PUT -H "Content-Type: application/json" --data-binary #somefile.json http://localhost:8000/home/search/3
Watch out for double quotes insides double quotes:
curl -iX PUT -H "Content-Type: application/json" -d '{"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}' localhost:8000/home/search/3
import json
import requests
def query_records():
args = {
"table_name": "instance_group",
"filter_list": [
[
{
"field": "account",
"op": "SCmp",
"value": "finder-others-live_group_file"
}
]
],
"order_field": "id",
"start": 0,
"length": 500,
"order_inc": True
}
url = "http://misc.yard.wx.com:80/api/v1.0/query_records"
resp = requests.post(url, data=json.dumps(args))
print resp
print resp.content
if __name__ == "__main__":
# url = "http://misc.yard.wx.com:80/api/v1.0/query_records"
query_records()
This is a python file for sending a post request,but now I need to use bash script to send this request.
Then I used the py2curl package to convert the request into a curl command.
curl -v -X POST -H "Accept: */*" -H "Accept-Encoding: gzip, deflate" -H "Connection: keep-alive" -H "Content-Length: 196" -H "User-Agent: python-requests/2.25.1" -d "{\"table_name\": \"instance_group\", \"filter_list\": [[{\"field\": \"account\", \"op\": \"SCmp\", \"value\": \"finder-others-live_group_file\"}]], \"order_field\": \"id\", \"start\": 0, "length": 500, \"order_inc\": true}" http://misc.yard.wx.com:80/api/v1.0/query_recordsop/
But when I execute the python file, I can receive a response, but there is no response when I execute this curl command. Why is it? And what should I do?
You can use the following parameters, from the curl documentation,
https://curl.se/docs/manpage.html#--trace
-S, --show-error
You can also use the following parameter,
-v, --verbose
Makes curl verbose during the operation. Useful for debugging and seeing what's going on "under the hood".
For even more output, you can use
--trace <file>
Enables a full trace dump of all incoming and outgoing data, including descriptive information, to the given output file. Use "-" as filename to have the output sent to stdout. Use "%" as filename to have the output sent to stderr.
I am trying to programmatically set up a cluster. For that I have followed the documentation (here https://docs.couchdb.org/en/stable/setup/cluster.html#the-cluster-setup-api), and created a quick python script that does it for me.
I then get the following output: {'error': 'setup_error', 'reason': 'Cluster setup unable to sync admin passwords'}. So I decided to try the Fauxton interface (which works) and see what goes over the line.
Made some changes to the python script to match exactly what Fauxton sends to the cluster... But were the manual interface (through Fauxton) works, I still get this "unable to sync" error when replicating what happens under the hood... This is driving me crazy now...
Using the following start script:
for i in {1..3}; \
do echo "$i"5984:5984; \
docker run -d --rm -p "$i"5984:5984 \
-e NODENAME=box0$i.couch -e ERL_FLAGS='-setcookie "brumbrum"' \
-e COUCHDB_USER=admin -e COUCHDB_PASSWORD=admin \
--name box0$i --network couch \
couchdb:3.1.1; \
done
which spins up three fresh docker containers using the 3.1.1 version of couchdb.
The script I use to clusterize you can find here: https://github.com/wasperen/couchdb_clusterify/blob/main/clusterize.py
Thanks for any help. But anyway: happy new year!
Just followed the documentation step by step, with the same result:
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"admin", "node_count":"3"}'
{"error":"bad_request","reason":"Cluster is already enabled"}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"admin", "port": 5984, "node_count": "3", "remote_node": "box02.couch", "remote_current_user": "admin", "remote_current_password": "admin" }'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "add_node", "host":"box02.couch", "port": 5984, "username": "admin", "password":"admin"}'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"admin", "port": 5984, "node_count": "3", "remote_node": "box03.couch", "remote_current_user": "admin", "remote_current_password": "admin" }'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "add_node", "host":"box03.couch", "port": 5984, "username": "admin", "password":"admin"}'
{"ok":true}
root#75c127f35ad6:~# curl -X POST -H "Content-Type: application/json" http://admin:admin#box01.couch:5984/_cluster_setup -d '{"action": "finish_cluster"}'
{"error":"setup_error","reason":"Cluster setup unable to sync admin passwords"}
And this from the browser logs, using the Fauxton user interface (on box01):
POST http://localhost:15984//_cluster_setup
DATA {"action":"enable_cluster","username":"admin","password":"admin","bind_address":"0.0.0.0","port":5984,"node_count":3,"singlenode":false}
RESPONSE 400 {"error":"bad_request","reason":"Cluster is already enabled"}
POST http://localhost:15984//_cluster_setup
DATA {"action":"enable_cluster","username":"admin","password":"admin","bind_address":"0.0.0.0","port":5984,"node_count":3,"remote_node":"box02.couch","remote_current_user":"admin","remote_current_password":"admin"}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"add_node","username":"admin","password":"admin","host":"box02.couch","port":5984,"singlenode":false}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"enable_cluster","username":"admin","password":"admin","bind_address":"0.0.0.0","port":5984,"node_count":3,"remote_node":"box03.couch","remote_current_user":"admin","remote_current_password":"admin"}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"add_node","username":"admin","password":"admin","host":"box03.couch","port":5984,"singlenode":false}
RESPONSE 201 {"ok":true}
POST http://localhost:15984//_cluster_setup
DATA {"action":"finish_cluster"}
RESPONSE 201 {"ok":true}
GET http://localhost:15984/_membership
RESPONSE 200 {"all_nodes":["couchdb#box01.couch","couchdb#box02.couch","couchdb#box03.couch"],"cluster_nodes":["couchdb#box01.couch","couchdb#box02.couch","couchdb#box03.couch"]}
I think there might be a bug in CouchDB 3.x where it does not accept the finish_cluster action when basic authentication is being used. Seems like it works perfectly fine if you get a session cookie first through the _session endpoint and then use that for authentication.
I checked your code out, it looks all good seems like you are just hitting this issue here: https://github.com/apache/couchdb/issues/2858. It can get worked around with a GET request to the cluster coordinator node.
I'm working with a third-party HTTP API like this:
curl https://secure.phabricator.com/api/maniphest.edit \
-d "api.token=${API_TOKEN}" \
-d "transactions[0][type]=subscribers.add" \
-d "transactions[0][value][0]=${OWN_USER_PHID}" \
-d "transactions[0][value][1]=${ANOTHER_USER_PHID}"
Basically it's a POST request with a request body like this (if written in Python syntax):
{"api.token": "...", "transactions": [{"type": "...", "value": ["...", "..."]}]
You can see that transactions is a nested array.
How do I encode such payload in Python? I'm using requests to make the request if that matters.