Please help me convert a working URL request in the web browser to a python request module executable request.
My working browser request URL:
https://192.168.100.25/api?type=config&action=set&xpath=/config/devices/entry[#name=%27localhost.localdomain%27]/network/interface/ethernet/entry[#name=%27ethernet1/1%27]/layer3/ip&element=%3Centry%20name=%279.6.6.6/24%27/%3E
This device basically accepts Rest API calls in XML format. Please help me, convert this to a python requests POST request.
I have found a way to do this with Python Requests:
url = '''https://192.168.100.25/api?
type=config&action=set&xpath=/config/devices&element='''
out = requests.post(url, verify=False, auth=HTTPBasicAuth(username, password))
This is working.
Please let me know if there is an easier and proper way to do this.
Related
I'm trying to fetch metadata from thoughtspot. I am able to call the url using browser and fetch the data. But here I'm trying to achieve it via python program. According to thougthspot documentation. I have to enable trusted authentication and pass my secret key & username to obtain a token which I can use in my program.
https://developers.thoughtspot.com/docs/?pageid=api-auth-session
my username : username#username.com
secret key : secret-key
Below is my code:(generated by postman)
import requests
url = "https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/session/auth/token?auth_token=secret-key&access_level=FULL&username=username#username.com"
payload={}
headers = {}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
I'm getting Bad request error. Anyone here using thoughtspot over this issue. Appreciate your support very much.
Error I'm getting:
{"type":"Bad Request","description":"The server could not understand the request due to invalid syntax."}
I can fetch data by calling the api using a web-browser. Below url returns list of all meta-data objects. I want to achieve this using a python program (I have to authenticate first & call the below URL - Authentication step is not working for me when I tried to follow the documentation)
https://<ThoughtSpot-host>/callosum/v1/tspublic/v1/metadata/list
Did you try changing the url so that it includes the domain name?
Also post the error you are getting. And a screenshot of a working request would be great!
Heyo. I'm trying to make a small application in my spare time that uses the Spotify API . I have managed to get my program to use oAuth 2 to let a user authorize my app to manipulate their Spotify, but I have run into a problem with a certain endpoint on the Spotify API.
The endpoint I am having trouble with is https://api.spotify.com/v1/me/player/play (here's a link to their docs for the endpoint https://developer.spotify.com/console/put-play/). Whenever I try to make a put request to the endpoint I receive a 400 status code with the message "Malformed json" I get this message even when I copy/paste their own json from the docs, so I don't think it's a problem with how I am formatting my json, besides I have used json before to call other endpoints and they haven't had a problem with my formatting on those calls.
Here is my code:
headers = {"Authorization":"Bearer {}".format(access_token)}
url = 'https://api.spotify.com/v1/me/player/play'
payload = {"context_uri": "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr"}
r = requests.put(url, headers=headers, data=payload)
print(r)
print(r.text)
To clarify, access_token is the access token that I have gotten from their authorization process, and I am using python-requests to make the http requests (Here is the docs for that: https://requests.kennethreitz.org/en/master/)
I am wondering if the problem is due to the fact that Spotify uses colons int their track IDs and colons are also used in JSON? I saw in another thread on here that I should try to add "Content-Type":"application/json" to my headers but that didn't change the outcome at all.
Any help is greatly appreciated, and if you need any more info please let me know. Thank you!
If your payload is a dict use json kwargs in requests lib. data works for string payload. Here you go:
r = requests.put(url, headers=headers, json=payload)
I have to work with an API which is a Swagger UI type, and I have the documentation of the API but seems like something is missing.
I make my request like this:
url = "https://something.net/some-other-part/api/devices/"
response = requests.get(url, verify=False, auth= ("xy", "xy"))
soup = bs(response.text)
I get back responses with code 200, so it's OK. BUT...
The response doesn't include everything I need, altough the response class says there is a lot of information I could except to get with the response.
Without knowing the response-class of the API, can you guys tell me how can you extend the URL or the request -with paramateres for example- to get more/more specific data?
Later on, I found on that the question was really not on point and solved the task on my own.
I'm start learning python with requests library and i get facebook for example.
This is my code:
import requests
get_response = requests.get(url='https://www.facebook.com/login/identify?
ctx=recover')
post_data = {'email':'mycorrectemailaddress'}
post_response = requests.post(url="https://www.facebook.com/login/identify?ctx=recover/POST", data=post_data)
print(post_response.text)
And my script not going to the next page, i don't know where is my fault.
You didn't pass all the required data.
payload = {lsd:AVpg3ZvY
email:your#email.com
did_submit:Search
__user:0
__a:1
__dyn:7AzHK4GgN1t2u6XgmwCwRAKGzEy4S-C11xG12wAxu13wIwHx27QdwPG2iuUG4XzEa8uwh9UcU88lwIwHwJwnoCcxG48hwv9FovgeFUuzUhws82BxCqUkguy99UK
__af:iw
__req:5
__be:-1
__pc:PHASED:DEFAULT
__rev:2929740}
Sadly, I don't know what are these args. But you could parse it from a get requrest on the page or use selenium for the informations, and pass cookies to the requests module, and then make the post request.
I decided to try to make an automated login script for Minecraft. However, the new authentication API is stumping me. I can't find any mentions of the new functionality of the API on here. This is my code as it stands:
import requests
import json
data = json.dumps({"agent":{"name":"Minecraft","version":1},"username":"abcdef","password":"abcdef","clientToken":""})
headers = {'Content-Type': 'application/json'}
r = requests.post('https://authserver.mojang.com', data=data, headers=headers)
print (r.text)
Unfortunately, this returns:
{"error":"Method Not Allowed","errorMessage":"The method specified in the request is not allowed for the resource identified by the request URI"}
According to this resource on request format, this error means that I didn't correctly send a post request. However, I clearly declared requests.post(), so my first question is how am I incorrect, and what is the correct way to go about this?
My second question is, since I'm relatively new to Python and JSON, how would I replace the username and password fields with my own data, inside a variable?
You haven't specified an endpoint in your POST request, for example:
https://authserver.mojang.com/authenticate
The root of the website probably does not accept POST requests
http://wiki.vg/Authentication#Authenticate