Python: How to keep single quotes in list/json - python

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"

Related

curl: could not resolve host

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

How to send this request with bash script instead of python?

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.

HTTP form-encoding payload with nested arrays

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.

What is the python requests equivalent of the following curl command

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.

No JSON object could be decoded - tastypie - curl

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/

Categories