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
Related
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 am creating a rest api in Python using flask and using curl command to test my function.
Curl Command:
$ curl -X POST
-H "Content-Type:application/json"
-d '{"keyword": "2''binders", "country": "xyz", "frequency": "1","url":"www.example.com"}' \
http://127.0.0.1:5000/google
Output:
'['New Request', u'**2binders**', u'xyz', u'www.example.com', u'1']'
As you can see I am passing inches in json 2''binders but in my list it is removing that.
Edit:
Python:
json = request.get_json(force=True)
print json
In your curl command, try using "keyword": "2\'\'binders". You need to escape the single quotes inside the -d parameter, because you're using single quotes around the -d parameter.
The shell is getting confused and thinking you are wanting string concatenation. E.g. the shell is seeing 'abc''def' and interpreting that as 'abcdef'.
If you try to json.dumps the dictionary itself:
>>> s = json.dumps(d)
>>>
>>> s
'{"url": "www.example.com", "country": "xyz", "frequency": "1", "keyword": "2\'\'binders"}'
^ ^
You will see that it will automatically escape ', as mentioned in the previous answer, escaping it should do the trick to you:
>>> json.loads(s)
{u'url': u'www.example.com', u'country': u'xyz', u'frequency': u'1', u'keyword': u"2''binders"} #Single quote is back
Got it!!!
curl -X POST -d '{"keyword": "2'\'''\''binders", "country": "xyz", "frequency": "1","url":"http://www.example.com/"}' http://127.0.0.1:5000/google -H "Content-Type:application/json"
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.
I was following the tastypie tutorial word for word until i reached the post part:
http://django-tastypie.readthedocs.org/en/latest/interacting.html#creating-a-new-resource-post
When i run this command i keep getting the following error:
No JSON object could be decoded
I checked and I am certain that I am following the documentation word for word.
Thanks for your help
Turned out to be a windows thing with cURL.
The JSON data should be quoted with double quotes ("") instead of single quotes.
All the double quotes in the json packet must be escaped with a backslash (\)
Eg: So, this:
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body": "This will prbbly be my lst post.", "pub_date": "2011-05-22T00:46:38", "slug": "another-post", "title": "Another Post", "user": "/api/v1/user/1/"}' http://localhost:8000/api/v1/entry/
Should be:
curl --dump-header - -H "Content-Type: application/json" -X POST --data "{\"body\": \"This will prbbly be my lst post.\", \"pub_date\": \"2011-05-22T00:46:38\", \"slug\": \"another-post\", \"title\": \"Another Post\", \"user\": \"/api/v1/user/1/\"}" http://localhost:8000/api/v1/entry/