I have a working script that does the job just fine but I can't seem to figure out how to print the status code after the script runs.
Can someone please review and provide some guidance and help?
import requests
import json
url = 'http://10.3.198.100/ins'
switchuser = 'user'
switchpassword = 'password'
myheaders = {'content-type' : 'application/json-rpc'}
payload = [
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "vrf context management",
"version": 1
},
"id": 1
},
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "ip route 192.168.255.0/24 10.3.198.130",
"version": 1
},
"id": 2
},
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "copy run start",
"version": 1
},
"id": 3
}
]
response = requests.post(url, data = json.dumps(payload), headers = myheaders, auth = (switchuser, switchpassword)).json()
You are immediately calling .json() after your .post() returns. This means that you are throwing away the rest of the info from the response.
Try this instead:
response = requests.post(
url,data=json.dumps(payload),
headers=myheaders,
auth=(switchuser,switchpassword))
json_response = response.json()
print(response.status_code)
Reference: http://docs.python-requests.org/
Related
I tried calling an API using both python and nodejs but the format in which the response is in is quite different.
For nodejs, it returns the statuscode, headers, body, request etc.
E.g.
(Only an example, not the actual response so you can ignore any syntax errors)
{
"statusCode" : 200,
"headers" : {
'xxxx' : 'xxxx"
},
"body" : {
"name" : "james",
"age" : 35
},
"request" {
"method" : "POST"
}
}
For python, it only returns the response body.
E.g.
(Only an example, not the actual response so you can ignore any syntax errors)
{
'name' : james,
'age' : 35
}
I am aware that i am able to get the headers etc using python request response object such as response.headers etc but is there a way that allows me to a similar response like nodejs. I know about response.dir but i need to forward this response so the format of response.dir is not accepted.
Thanks all!
You can compile your own response, similar to what you get from nodejs, something like
import json
import requests
url = 'http://date.jsontest.com/'
r = requests.get(url)
j = {
"statusCode": r.status_code,
"headers": dict(r.headers),
"body": r.json(),
"request": {
"method": r.request.method
}
}
print(json.dumps(j, indent=4))
Will produce:
{
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
"X-Cloud-Trace-Context": "9b4e313aa5e6373ddc997dc963ee8f1b",
"Date": "Thu, 23 Jun 2022 03:11:27 GMT",
"Server": "Google Frontend",
"Content-Length": "100"
},
"body": {
"date": "06-23-2022",
"milliseconds_since_epoch": 1655953887719,
"time": "03:11:27 AM"
},
"request": {
"method": "GET"
}
}
I'm trying to validade an address in FedEX API using Python 3.8 and it returns an error of invalid field value
First I connect to the Auth API
payload={"grant_type": "client_credentials",'client_id':Client_id,'client_secret':Client_secret}
url = "https://apis-sandbox.fedex.com/oauth/token"
headers = {'Content-Type': "application/x-www-form-urlencoded"}
response=requests.post(url, data=(payload), headers=headers)
And it returns a message with the Auth token correctly
{"access_token":"eyJhbGciOiJSUzI1NiIsInRM5U0F2eUs1ZVFBVTFzS5k","token_type":"bearer","expires_in":3599,"scope":"CXS SECURE"}
Then I just get the token to use it in next transactions
token = json.loads(response.text)['access_token']
Then I prepare the next payload for address validation API
payload_valid_address = {
"addressesToValidate": [
{
"address":
{
"streetLines": ["7372 PARKRIDGE BLVD"],
"city": "Irving",
"stateOrProvinceCode": "TX",
"postalCode": "75063-8659",
"countryCode": "US"
}
}
]
}
And send the request to the new endpoint with the given token
url = "https://apis-sandbox.fedex.com/address/v1/addresses/resolve"
headers = {
'Content-Type': "application/json",
'X-locale': "en_US",
'Authorization': 'Bearer '+ token
}
response = requests.post(url, data=payload_valid_address, headers=headers)
print(response.text)
and get the error
<Response [422]>
{"transactionId":"50eae03e-0fec-4ec7-b068-d5c456b64fe5","errors":[{"code":"INVALID.INPUT.EXCEPTION","message":"Invalid field value in the input"}]}
I have made inumerous tests and I don't get the invalid field.
Anyone know what is happening and can help?
payload = json.dumps({input payload}) this works to prevent the response error 422
<Response [422]>
I have fixed it
For any reason, convert the string payload_valid_address to Json in 2 steps doesn't work
payload_valid_address = {....}
payload = json.dumps(payload_valid_address)
But if made in just one step it pass the API request
payload_valid_address = json.dumps({....})
I also had this error and additionally had to use json.loads(json_string) when the json data contained key words like false, true or null.
import json
json_string = r'''{
"accountNumber": {
"value": "802255209"
},
"requestedShipment": {
"shipper": {
"address": {
"postalCode": 75063,
"countryCode": "US"
}
},
"recipient": {
"address": {
"postalCode": "m1m1m1",
"countryCode": "CA"
}
},
"shipDateStamp": "2020-07-03",
"pickupType": "DROPOFF_AT_FEDEX_LOCATION",
"serviceType": "INTERNATIONAL_PRIORITY",
"rateRequestType": [
"LIST",
"ACCOUNT"
],
"customsClearanceDetail": {
"dutiesPayment": {
"paymentType": "SENDER",
"payor": {
"responsibleParty": null
}
},
"commodities": [
{
"description": "Camera",
"quantity": 1,
"quantityUnits": "PCS",
"weight": {
"units": "KG",
"value": 20
},
"customsValue": {
"amount": 100,
"currency": "USD"
}
}
]
},
"requestedPackageLineItems": [
{
"weight": {
"units": "KG",
"value": 20
}
}
]
}
}'''
data = json.loads(json_string)
data
response = requests.request("POST", url, data=json.dumps(data), headers=headers)
I have managed to get Fedex to accept my ayload, however i get this message:
"code":"INVALID.INPUT.EXCEPTION","message":"Validation failed for object='shipShipmentInputVO'. Error count: 1","parameterList":[{"key":"NotNull.shipShipmentInputVO.labelResponseOptions","value":"labelResponseOptions cannot be null"
and when I add the required parameter
'labelResponseOptions': "URL_ONLY"
I get this:
"code":"SYSTEM.UNEXPECTED.ERROR","message":"The system has experienced an unexpected problem and is unable to complete your request. Please try again later. We regret any inconvenience."
Is this really some Fedex internal error?
I have tried to send POST requests to my slack channel using webhooks to no avail.
It always returns a bad request no matter what I do. Is there a way to send a POST request to slack without using webhooks?
EDIT: Code that I'm using
import json
import urllib.request
#import botocore.requests as requests
def lambda_handler(event, context):
webhook=event['webhook']
#response = urllib.request.urlopen(message)
#print(response)
slack_URL = 'https://hooks.slack.com/services/mywebhookurl'
# req = urllib.request.Request(SLACK_URL, json.dumps(webhook).encode('utf-8'))
json=webhook
json=json.encode('utf-8')
headers={'Content-Type': 'application/json'}
#urllib.request.add_data(data)
req = urllib.request.Request(slack_URL, json, headers)
response = urllib.request.urlopen(req)
I think the problem arises when you encode your JSON in utf-8. Try the following script.
import json
import requests
# Generate your webhook url at https://my.slack.com/services/new/incoming-webhook/
webhook_url = "https://hooks.slack.com/services/YYYYYYYYY/XXXXXXXXXXX"
slack_data = {'text': "Hi Sarath Kaul"}
response = requests.post(webhook_url, data=json.dumps(slack_data),headers={'Content-Type': 'application/json'})
print response.status_code
If you want to use urllib
import json
import urllib.request
import urllib.parse
url = 'https://hooks.slack.com/services/YYYYYYYYY/XXXXXXXXXXX'
data = json.dumps({'text': "Sarath Kaul"}).encode('utf-8') #data should be in bytes
headers = {'Content-Type': 'application/json'}
req = urllib.request.Request(url, data, headers)
resp = urllib.request.urlopen(req)
response = resp.read()
print(response)
Without using any extra lib(like requests), one can still do GET/POST using urllib build-in python3 module. Below is the example code:
def sendSlack(message):
req_param= {"From":"","Time":"","message":message}
slack_data = {
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Message",
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*From:*\n{}".format(req_param['From'])
},
{
"type": "mrkdwn",
"text": "*Time:*\n{}".format(req_param['Time'])
}
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Message:*\n{}".format(req_param['message'])
}
]
}
]}
req = request.Request("https://hooks.slack.com/services/<COMPLETE THE URL>", data=json.dumps(slack_data).encode('utf-8')) # this will make the method "POST"
resp = request.urlopen(req)
print(resp.read())
Make sure to send the right payload, on slack. This code will work like a charm for AWS LAMBDA too. Please see the example below:
import json
from urllib import request
def sendSlack(message):
req_param= {"From":"","StartTime":"now","DialWhomNumber":message}
slack_data = {
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Message",
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*From:*\n{}".format(req_param['From'])
},
{
"type": "mrkdwn",
"text": "*Time:*\n{}".format(req_param['Time'])
}
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Message:*\n{}".format(req_param['message'])
}
]
}
]}
req = request.Request("https://hooks.slack.com/services/<COMPLETE THE URL>", data=json.dumps(slack_data).encode('utf-8')) # this will make the method "POST"
resp = request.urlopen(req)
print(resp.read())
def lambda_handler(event, context):
# TODO implement
try:
print("-->GOT A REQUEST",event['queryStringParameters'])
sendSlack(event['queryStringParameters']['message'])
return {'body':json.dumps({'status':200,'event':'accepted'})}
except Exception as e:
print("Exception happenned",e)
return {
'statusCode': 400,
'body': json.dumps({'status':400,'event':'Something went wrong'})
}
I am trying to integrate Skype interview API using python and JWT.
for this 1st I am creating a token using python and jwt, and after that, I am sending API request call to skype interview with generated above token.
the request is processed and I am getting a proper response. the scheduling is set to automatic and date proposing is set to the candidate.
my issue is instead of the candidate to get the date proposing email the interviewer is getting the date selecting email without even proposing the date by the candidate.
You can see/check below my code:
input_data = {
"iemail": "interviewer#email.com",
"iduration": 90,
"cname": "Candidate",
"location": "LOCAL"
"position": "Full Stack Developer",
"cemail": "candidate#email.com",
"iname": "Interviewer"
}
def payloadGenertor(content):
jti = uuid.uuid1()
iss = "ISSUER"
iat = int(datetime.now().timestamp())
sub = hash256(content)
exp = iat + 10
payload = {
"jti": f '{jti}',
"iss": f '{iss}',
"iat": iat,
"sub": f '{sub}',
"exp": exp
}
payloadStr = json.dumps(payload)
return payloadStr
url = "https://interviews.skype.com/api/interviews"
content = {
"position": {
"code": input_data['location'] + " " + str(uuid.uuid1()).split('-')[0],
"title": input_data['position'],
"Description": f "Interviews for {input_data['position']} {input_data['location']}."
},
"participants": [
{
"name": input_data['iname'],
"email": input_data['iemail'],
"role": "interviewer",
"timezone": "Asia/Kolkata"
},
{
"name": input_data['cname'],
"email": input_data['cemail'],
"role": "candidate",
"timezone": "Asia/Kolkata"
}
],
"scheduling": {
"duration": int(input_data['iduration']),
"mode": "automatic",
"dateproposing": "candidate"
}
}
secret = "API_SECRET"
payloadHeader = {
"alg": "HS256",
"typ": "JWT"
}
payload = payloadGenertor(content)
jwt = generate_JWT(payloadHeader, payload, secret)
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + jwt
}
response = requests.post(url, data = content, headers = headers)
print(response.text)
#!/usr/bin/python
import requests
import uuid
random_uuid = uuid.uuid4()
print random_uuid
url = "http://192.168.54.214:8080/credential-store/domain/_/createCredentials"
payload = '''json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "random_uuid",
"username": "testuser3",
"password": "bar",
"description": "biz",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}'''
headers = {
'content-type': "application/x-www-form-urlencoded",
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
In the above script, I created a UUID and assigned it to the variable random_uuid. I want the UUID that was created to be substituted inside json for the value random_uuid for the key id. But, the above script is not substituting the value of random_uuid and just using the variable random_uuid itself.
Can anyone please tell me what I'm doing wrong here?
Thanks in advance.
You'll can use string formatting for that.
In your JSON string, replace random_uuid with %s, than do:
payload = payload % random_uuid
Another option is to use json.dumps to create the json:
payload_dict = {
'id': random_uuid,
...
}
payload = json.dumps(payload_dict)
Use str.format instead:
payload = '''json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "{0}",
"username": "testuser3",
"password": "bar",
"description": "biz",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}'''.format(random_uuid)
You could use direct JSON entry into a dict:
payload = {
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": random_uuid,
"username": "testuser3",
"password": "bar",
"description": "biz",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}
This code may help.
#!/usr/bin/python
import requests
import uuid
random_uuid = uuid.uuid4()
print random_uuid
url = "http://192.168.54.214:8080/credential-store/domain/_/createCredentials"
payload = '''json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "%s",
"username": "testuser3",
"password": "bar",
"description": "biz",
"$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
}
}''' % random_uuid
headers = {
'content-type': "application/x-www-form-urlencoded",
}
print payload
print(response.text)