How can I test the user service from the command-line using curl?
Let's say I have a protoRPC service running and I issue something like:
curl -H 'content-type:application/json' -d '{"name":"test1"}' http://localhost:8080/api.context_create
but this service requires a logged-in user. How can I simulate one?
It looks like you need to do it in two steps (see this link for the details). The first is submitting a request to:
$ curl http://localhost:8000/_ah/login -d "email=youremail#here.com&action=Log+In" -c -
The response will look something like this:
localhost FALSE / FALSE 0 dev_appserver_login "youremail#here.com:False:0123456789101112"
You can then submit a request to your target page, using the cookie provided. In your case (assuming the above cookie) you could try:
curl -H 'content-type:application/json' \
-d '{"name":"test1"}' \
-b "dev_appserver_login="youremail#here.com:False:0123456789101112"" \
http://localhost:8080/api.context_create
Related
Im doing this curl request to get some data in python , how can i get session id of curl request so that i can reuse again.
commands.getoutput("curl -H \"Content-Type:application/json\" -k -u username:password -X GET https://10.39.11.4/wapi/v2.7/member -s"
curl has a built-in cookie jar meant for storing just the cookies and sending them back to the server.
To store cookies in the cookie jar we use the -c flag and give it the name of a file we wish to store the cookies in.
$ curl -X POST -c cookies.txt -u "user1:password1" website.com/login
$ cat cookies.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_quiet-waters-1228.herokuapp.com FALSE / FALSE 0 _curl_test_app_rails_
session cm53d2RJN1VncV........
There you can find session ID .
As Mentioned by Daniel Stenberg (founder of cURL):
use -b cookies.txt in the subsequent curl command line to make use of those cookies
You should check out the requests library, it has a Session management described here: http://docs.python-requests.org/en/master/user/advanced/#session-objects
s = requests.Session()
s.auth = ('username', 'password')
s.headers.update({'Content-Type': 'application/json'})
r = s.get('https://10.39.11.4/wapi/v2.7/member')
If you want to save a session and then load it, you should use dict_from_cookiejar and cookiejar_from_dict like this:
# Save session
s_dict = requests.utils.dict_from_cookiejar(s.cookies)
# Load session
cookies = requests.utils.cookiejar_from_dict(s_dict)
s = requests.session(cookies=cookies)
I am trying to convert curl commands to python requests using python scripts. I could find the uncurl modules and could use it to convert few curl commands successfully. But I am facing issues to make my script generic for all curl commands. Following issues are holding back my work. I am trying to write the utility in python which will take curl commands from the text file and will convert commands to python requests one by one.
Commands types like GET / POST etc are not accepted by uncurl.
Curl command options like -u , -X etc are rejected.
For DELETE requests, how should I use uncurl.
curl -v -k -H "Content-Type: application/json" -H "sync_id: 00000000-
0000-0000-0000-000000000001" -H "sync_token:
NhbSzPhbtFlZ9Gm1nLr5f8e0WLGQitG4o00jb006m5Vcs00XVqzRdHcFtyv4YOzd5S02Z3x1iR5OWQINgLP2Og" -H "instance_id: instance_id" -X GET 'example.com'
curl -v -k -H "Content-Type: application/json" -H "sync_id: 00000000-0000-0000-0000-000000000001" -H "sync_token: NhbSzPhbtFlZ9Gm1nLr5f8e0WLGQitG4o00jb006m5Vcs00XVqzRdHcFtyv4YOzd5S02Z3x1iR5OWQINgLP2Og" -H "instance_id: instance_id" -X POST -d '[{"id":"1", "name":"1", "env_mapping_name":"a", "env_mapping_id":"a1"}]' example.com'
The python code which I used for convertsion.
import uncurl
command = 'curl -v -k -H "Content-Type: application/json" -H "sync_id: 00000000-0000-0000-0000-000000000001" -H "sync_token: NhbSzPhbtFlZ9Gm1nLr5f8e0WLGQitG4o00jb006m5Vcs00XVqzRdHcFtyv4YOzd5S02Z3x1iR5OWQINgLP2Og" -H "instance_id: instance_id" -X GET 'example.com'
print uncurl.parse(command)
I have stripped off the tags and curl options which were giving me exceptions as follows.
def stripTags(command):
'''Strip off the unwanted tags from the curl command'''
print '\nCommand is *********************************', command
command = command.replace('-X', '')
command = command.replace('-k', '')
command = command.replace('-v', '')
command = command.replace(' \'', ' \"')
command = command.replace('\' ', '\" ')
command = command.replace('\'', '\"')
command = command.replace(' GET ', '')
command = command.replace(' POST ', '')
command = command.replace(' PUT ', '')
command = command.replace(' POST ', '')
command = command.replace(' DELETE ', '')
print '\nStripped string is =============', command
return command
When I used the following curl command for conversion, I got an exception.
'curl -v --proxy-user "00dcf6e7-4513-4e46-bbaf-ef4cac8f8d47":"XUmh8pIG68Zo" -X GET "example.com" --proxy 127.0.0.1:8080 -k'
The -U option here is bothering. Likewise there are other options which I observed exceptions on. So I am not sure if I am using uncurl correctly or not.
All the curl commands are converted when I used online convertor from curl.trillworks.com.
Can you please provide me the pointer on how to convert curl commands in to python requests?
**** :- I could able to send the curl command using 'runcurl' package. As per my understanding, the uncurl package has limitation of sending the curl request which the runcurl bridges by providing the 'execute' method. I used the following code to do so.
import runcurl
cmd = "curl -v -u "00dcf6e7-4513-4e46-bbaf-ef4cac8f8d47":"XUmh8pIG68Zo" -X GET "https://example.com" -k"
#strip off the curl options like -k, -v, -X, GET which causes runcurl to #throw exception.
try:
code = runcurl.execute(command)
except:
code = 400
//Write the failed cases in some file for further references
As earlier, I have to strip off the options supported by curl like '-k', '-v'. In the above curl command, the option for authentication is provided with the '-u' option. But the runcurl is throwing an exception.
I am seeking help on this point. My expectation is that 'runcurl' should not error out for the required options supported by curl. For ex:- '-u' option. Did I miss anything here? Is there any better way of handling curl command above?
Note :- I am trying to avoid the use of 'subprocess.call()' to call the curl command in my code.
When i use curl under proxy for rest framework api testing in django i'm getting this error Exception Value: Unable to parse connection string: ""
i have used
curl -v --noproxy localhost -X POST -H "Authorization: Token <token id >" -d "email=d#c.co" "http://localhost:4000/api/....."
and also tried
export http_proxy=""
while was working properly when i was not under proxy
curl -X POST -H "Authorization: Token <token id >" -d "email=d#c.co" "http://localhost:4000/api/....."
From the manual of --noproxy, it expects -
Comma-separated list of hosts which do not use a proxy, if one is specified. The only wildcard is a single * character, which matches all hosts, and effectively disables the proxy.
So you must specify a comma separated list of hosts. That may be either specific hosts such as "localhost:4000" or "*".
So the following should work-
curl -v --noproxy "*" localhost -X POST -H "Authorization: Token <token id >" -d "email=d#c.co" "http://localhost:4000/api/....."
Hi I use following curl commands to fetch information , here I get session ID and csrf token and then proceed further to do post login and any request.
1. Get /login/, show headers to get the cookies but don't care about output:
curl -o /dev/null -D - 'https://192.168.2.1/login/' --insecure
2. Post /login/. Note how we set the cookies with '-b', pass the parameters with '-d' and set an HTTP Referer header with '-e'.
curl -o /dev/null -D - -b 'csrftoken=TOKEN;sessionid=ID' \
-d 'csrfmiddlewaretoken=TOKEN' \
-d 'username=USER' \
-d 'password=PASSWORD' \
-e 'https://192.168.2.1/login/' \
'https://192.168.2.1/login/'
3. Get list of projects (/api/project/). Here we do care about the output.
curl -D - -b 'csrftoken=TOKEN;sessionid=ID' \
'https://192.168.2.1/api/project/?format=json&limit=0'
How can I automate the same using python , may be any example usingn request or pycurl
I have some code that used to work before the Drf 3.3 / Django 1.8 combo:
I'd have a serializer like this:
class MySerializer(...):
dict_field = serializers.DictField(child=serializers.CharField())
.....
The following used to work previously, but now dict field is empty:
curl http://server.com/api/endpoint -H "Authorization: Token <XXX>" -d other_field=ABC -d dict_field.key1=val1 -d dict_field.key2=val2
The code for DictField seems like it expects the data in this format, but it's not working. Other non dict fields work, and using json works, but for separate reasons, I must use HTTP POST. Is this a bug with the latest versions?
you can post dictionary data like :
curl -H "Content-Type: application/json" -H "Authorization: Token <XXX>" -X POST http://server.com/api/endpoint -d '{"other_field": "ABC","dict_field": {"key1":"val1","key2":"val2"}}'