creating a request to replace curl -XDelete - Python - python

I'm executing the following cURL command to clean an Elastic Search index:
curl -s -XDELETE "http://<server>/<server_index>/<index>/_query" -d '{
"query":{"match_all":{}}}
}'
However, I don't to execute a shell script or a subprocess command. Instead, I want to rely on requests to achieve this:
import requests
r = requests.get('http://<server>/<server_index>/<index>/_query')
How can I accomplish this?

The equivalent requests code should be something like:
data = {"query": {"match_all": {}}}
import requests
r = requests.delete("http://<server>/<server_index>/<index>/_query", data=data)

Related

python upload file with curl

i'm trying to upload an apk to server
the code i used to upload using curl is written like this:
curl -u "musername:mypass" -X POST "https://myurl/upload" -F "file=#D:\path\to\location.apk"
i tried to make a script using python with requests library like this:
response = requests.post(
self.urlUpload,
files={"file" : open(self.apkLocation, 'r')},
auth=(self.BSUsername, self.BSToken)
)
but it gives me errors:
{"error":"Malformed archive"}
anyone knows why this erros appeared?
Have you had a chance to try something like below?
import requests
files = {
'file': ('/path/to/app/file/Application-debug.apk', open('/path/to/app/file/Application-debug.apk', 'rb')),
}
response = requests.post('https://api-cloud.browserstack.com/app-automate/upload',
files=files,
auth=('BSUsername ', 'BSToken'))
print (response.text)#THE APP URL (bs://<hashed appid>) RETURNED IN THE RESPONSE OF THIE CALL
Check the BrowserStack REST API doc here

Callling a REST API in python

I want to call a REST api and get some json data in response in python.
curl https://analysis.lastline.com/analysis/get_completed -X POST -F “key=2AAAD5A21DN0TBDFZZ66” -F “api_token=IwoAGFa344c277Z2” -F “after=2016-03-11 20:00:00”
I know of python request, but how can I pass key, api_token and after? What is -F flag and how to use it in python requests?
Just include the parameter data to the .post function.
requests.post('https://analysis.lastline.com/analysis/get_completed', data = {'key':'2AAAD5A21DN0TBDFZZ66', 'api_token':'IwoAGFa344c277Z2', 'after':'2016-03-11 20:00:00'})
-F stands for form contents
import requests
data = {
'key': '2AAAD5A21DN0TBDFZZ66',
'api_token': 'IwoAGFa344c277Z2',
'after': '2016-03-11',
}
response = requests.post('https://analysis.lastline.com/analysis/get_completed', data=data)
-F means make a POST as form data.
So in requests it would be:
>>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})

How to use Python to execute complex CURL command for InfluxDB

Could I anyone explain how I can use requests in Python to send this CURL:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
I dont know how to parse it into a sample code. Thanks a lot
import requests
url_string = 'http://localhost:8086/write?db=mydb'
data_string = 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
r = requests.post(url_string, data=data_string)
To create a InfluxDB via Python3:
#!/usr/bin/env python3
import urllib.request as requests
url = "http://localhost:8086/query"
params = {
"q=CREATE DATABASE mydb"
}
r = requests.Request(url, data=params)

python curl for assembla

I am trying to port the following curl call into a python script using urllib/urllib2 :
curl -H "X-Api-Key: ccccccccccccccccccccccc" -H "X-Api-Secret: cbcwerwerwerwerwerwerweweewr9" https://api.assembla.com/v1/users/user.xml
I tried using the standard url call, but it failed:
url_assembla = 'https://api.assembla.com/v1/users/suer.xml'
base64string_assembla = base64.encodestring('%s:%s' % ('ccccccccccccccccccccccc','cbcwerwerwerwerwerwerweweewr9')).replace('\n', '')
req_assembla = urllib2.Request(url_assembla)
req_assembla.add_header("Authorization", "Basic %s" % base64string_assembla)
ET.parse(urllib2.urlopen(req_assembla))
Can any one advice on how to incorporate the Api-secret and Api-key. I want to do this as a script, so did not want to install the assembla package.
Thanks
If the curl command above works, you should perhaps just add the same headers into the urllib2 query?
Try this instead of the add_header("Authorization") line:
req_assembla.add_header("X-Api-Key", 'ccccccccccccccccccccccc')
req_assembla.add_header("X-Api-Secret", 'cbcwerwerwerwerwerwerweweewr9')

Python Requests Put not working with build.phonegap.com

I am making a python build script for a phonegap project.
I need to open the ios key before i build
I am trying to do this with a http put request through the requests module for python.
If i do it with cURL from command line, it works fine
curl -vvv -d 'data={"password":"myPassWord"}' -X PUT https://build.phonegap.com/api/v1/keys/ios/193686?auth_token=passwordlesstokenphg
But from python like this.
password_for_key = {'password': 'myPassword'}
authentication_token = {'auth_token': 'passwordlesstokenphg'}
requests.put('https://build.phonegap.com/api/v1/keys/ios/193686', data=password_for_key, params=authentication_token)
It just returns the json you would recieve if you did a cURL without the data.
For me it seems like the data is not being sent to phonegap correctly.
API reference from build.phonegap.com
docs.build.phonegap.com/en_US/2.9.0/developer_api_write.md.html
Please help :)
So when you do
curl -d "..." -X PUT https://example.com
curl sends exactly what's in that string. requests does not translate so directly to curl. To do something similar in requests you need to do the following:
import json
password_for_key = {'password': 'myPassword'}
authentication_token = {'auth_token': 'passwordlesstokenphg'}
requests.put('https://build.phonegap.com/api/v1/keys/ios/193686',
data={'data': json.dumps(password_for_key)},
params=authentication_token)
What requests will do is build data={"password":"myPassword"} for you if you use the above. First you have to JSON encode the data in password_for_key then pass it in the dictionary to data.

Categories