It would be a great help if somebody helps me to understand how the following command can be executed using python:
curl -X POST https://insights-collector.newrelic.com/v1/accounts/YOUR_ACCOUNT_ID/events -H "Content-Type: application/json" -H "X-Insert-Key: YOUR_KEY_HERE" -d '{"eventType":"Custom Event Name", "attribute1": "value"}'
SQL query results need to be converted to JSON format and need to be pushed to new relic using the above command.
try doing this
import requests
headers = {
'Content-Type': 'application/json',
'X-Insert-Key': 'YOUR_KEY_HERE',
}
data = '{"eventType":"Custom Event Name", "attribute1": "value"}'
response = requests.post('https://insights-collector.newrelic.com/v1/accounts/YOUR_ACCOUNT_ID/events', headers=headers, data=data)
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'm trying to get thread replies when somebody posts a message to a private slack channel using the python slack api, but I keep getting the following error:
{"ok":false,"error":"internal_error","warning":"missing_charset",
"response_metadata":{"warnings":["missing_charset"]}}
The scopes for my user token are: channels:history, groups:history, and chat:write.
Here's my code:
slack_client = WebClient(token='xoxp-xxxx-xxxx-xxxx-xxxx')
resp = slack_client.api_call(api_method="conversations.replies",
json={"channel": 'xxxx',
"ts": received_data['event']['thread_ts'],
"token": 'xoxp-xxxx-xxxx-xxxx-xxxx'})
I get the same error using curl:
curl -X POST -H 'Authorization: Bearer xoxp-xxxx-xxxx-xxxx' -H "Content-type: application/json"
--data '{"token": "xoxp-xxxx-xxxx-xxxx-xxxx","ts":"1595895414.009700", "channel": "xxxx"}'
https://slack.com/api/conversations.replies
I think you have to use GET method
you can add http_verb="GET" in parameter to api_call method
There is also a conversations_replies method that wraps this call
Thanks! Here's what my code ended up looking like:
slack_client = WebClient(token='xoxp-xxxx-xxxx-xxxx-xxxx')
resp = slack_client.api_call(api_method="conversations.replies",
params={"channel": 'xxxx',
"ts": received_data['event']['thread_ts'],
"token": 'xoxp-xxxx-xxxx-xxxx-xxxx'},
http_verb="GET")
Trying to re-write shell curl to python script with requests module...
My shell script :
#!/bin/bash
ip=$1
url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus
curl --silent -X POST "$url/api/computers/search?expand=$expand" -H "Content-Type: application/json" -H "api-secret-key: $secret" -H "api-version: v1" -d '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": "'"$ip"'"}]}' -k > $file
above works fine in bash.
I am trying to convert to similar python equivalent
What I tried
import json
import requests
import sys
ip = sys.argv[1]
sys_info = []
url=https://xxx.xx.xx.com
secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
expand=computerStatus
headers = {
'Content-Type': 'application/json',
'api-secret-key': secret,
'api-version': 'v1',
}
params = (
('expand', 'expand'),
)
data = '{"maxItems": 10,"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": ip}]}'
response = requests.post('https://url/api/computers/search?expand=expand', headers=headers, params=params, data=data)
print(response)
<Response [400]>
I am getting 400 response.. not sure where i am missing in syntax...
data needs to be a dictionary, try this:
data = {
"maxItems": 10,
"searchCriteria": [{"fieldName": "hostName","stringTest": "equal", "stringValue": ip}]
}
Also convert params to a dictionary as well:
params = {"expand" : expand}
And when posting:
response = requests.post(f'https://{url}/api/computers/search', headers=headers, params=params, data=data)
### or alternatively you can
response = requests.post(f'https://{url}/api/computers/search?expand={expand}', headers=headers, data=data)
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'd like to get this curl command to run in a Python Script
curl -H "public-api-token: 049cxxxxxc026ef7364fa38c8792" -X PUT -d "urlToShorten=google.com" https://api.shorte.st/v1/data/url
but I have now clue how.
def make_tiny(url):
connection = httplib.HTTPSConnection('api.shorte.st', 443)
connection.connect()
connection.request('PUT', '/v1/data/url', json.dumps({
"urlToShorten": url,
}), {
"public-api-token": "049cd75ad7axxx26ef7364fa38c8792",
"Content-Type": "application/json"
})
results = json.loads(connection.getresponse().read())
return results.get("shortenedUrl", "none").decode('utf-8')
Thanks anyway!