Sending Post request error
Hi talented developers. I am going to send request to specified url with authorization header.
I am trying to send POST request to
POST https://server/api HTTP 1.1
Authorization: Bearer a12b34567c89012def34g56789hi0j12
{
"data" : "test"
}
url = "https://server/api"
headers = {'Authorization' : 'Bearer %s' % API_KEY}
data = {
"data" : "test"
}
req = requests.post (url, headers = headers, data =
json.dumps(data))
Could you tell me what I am doing wrong in this code snippet?
I appreciate your help.
Thanks
Related
I am new to using this type of trying to get the user details in my org using python flask server by hitting the
URL : https://graph.microsoft.com/v1.0/me/
Request headers:
'Content-Type': "application/json", 'Authorization': accessToken
Response:
Access Denied
The page you requested has been blocked
any suggestions please?
From the graph URL , able to get the response from python code base we got the Access Denied.
method in python:
enter code here
def get_user_details_(access_token: str):
url = "https://graph.microsoft.com/v1.0/me"
headers = {'Content-Type': "application/json",
'Authorization': access_token
}
response = requests.get(url, headers=headers, verify=False)
print("response **** ", response.text)
response = response.json() if response else {}
return response
enter image description here
To resolve the above issue, We can follow the below workaround;
Make sure that you have following permissions given for your application so that, while calling the API it can able to read and give the desired result.
OUTPUT RESULT FOR REFERENCE:-
For complete setup please refr this BLOG|Querying Microsoft Graph API with Python by #Ephraim Mwai.
I found the answer.
It is related to Proxy. We need to add proxy details to request object.
proxyDict = {
"http": "http proxy address",
"https": "https proxy address"
}
response = requests.request('GET', url, headers=headers, proxies=proxyDict, verify=False)
Hi i'm trying to add a user to my guild using Discord's API.
The Scope I'm using is Guild.Join, Identify and Guilds. identify%20guilds%20guilds.join
The URL Is https://discordapp.com/api.
Here's my code:
#staticmethod
def add_to_guild(access_token, userID):
url = f"https://discordapp.com/api/guilds/{guildId that I cant show}/members/{userID}"
botToken = "<Bot Token I can't SHow haha>"
headers = {
"Authorization" : f"Bot {botToken}",
'Content-Type': 'application/json'
}
payload = {
'access_token' : access_token
}
response = requests.put(url=url, data=payload, headers=headers)
print(response.text)
when I call this method, I receive this error:
{"message": "400: Bad Request", "code": 0}
I've been through the Discord's Documentation countless of times and searched the internet to no avail.
Can someone please help ? Thanks.
you had to replace data to json
def add_to_guild(access_token, userID):
print(userID)
url = f"{Oauth.discord_api_url}/guilds/<GuildID>/members/{userID}"
headers = {
"Authorization" : "Bot <bottoken>",
"Content-Type": "application/json"
}
payload = {
'access_token' : str(access_token)
}
response = requests.put(url=url, json=payload, headers=headers)
print(response.text)
The userID is enough I am not sure why you want the payload or access token; Try to drop the payload part.
If you are trying to add a user, the user has to agree to grant you some permissions check here https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes so the token I think will be the "Bearer" tokens. Check the doc I've shared.
i'm working on settin up a rest api with python, however i'm having some problem getting it to work.
I'm working with the TV DB rest api: https://api.thetvdb.com/swagger
and using python with Requests library to pull out the information.
My code is currently:
import json
import requests
URL = "https://api.thetvdb.com/"
API_KEY = "Api_key"
USER_KEY = "Key"
USERNAME = "Name"
headers = {"Accept": "application/json"}
params = {
"apikey": API_KEY,
"userkey": USER_KEY,
"username": USERNAME
}
resp = requests.post(URL + "login/", headers = headers ,params=params)
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
So far i'm only getting error code 401, not sure why.
Solved:
2 Things needed to be changed
1. The resp was changed into:
resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))
The header had to have
"Content-Type": "application/json"
added to it :) It's now working, thanks everyone
The login parameters probably need to be a JSON-encoded string POSTed as the body of the message.
Try resp = requests.post(URL + "login/", headers = headers, data=json.dumps(params))
I tried to get successful login post request, but don't understand why do i always get status code 405. I checked many tutorials and all of them worked good for some examples.
import requests
import urllib.parse
def logintest(usr, pswd):
link = 'link'
headers = { 'Content-Type' : 'application/x-www-form-urlencoded',
'Server' : 'Apache-Coyote/1.1',
'Transfer-Encoding' : 'chunked'
}
payload = urllib.parse.urlencode({
'username': usr,
'password': pswd
})
responseget = requests.get(link)
print (responseget.status_code, responseget.reason)
if responseget.status_code == 200:
responsepost = requests.post(link, params = payload, headers = headers)
print (responsepost.status_code, responsepost.reason)
logintest('username', 'password')
Because the elibrary/home endpoint does not support POST requests. You probably want to send the request to http://81.180.75.144:8080/elibrary/auth/login, like the system does.
I have this code, it sends a request to my server which is run by web.py:
url = "https://sample.com/api"
auth_string = 'Basic ' + base64.encodestring('%s:%s' % ("usernamexxxx", "codexxxx"))[:-1]
headers = {"Content-Type": "application/json", "authorization": auth_string}
data = {"message":"WELCOME!..."}
req = urlfetch.fetch(url, method=urlfetch.POST, payload= simplejson.dumps(data), headers= headers)
response = req.content
So when I receive this kind of request in my web server I want to check if the username and code input to the request is valid or not? And how can I access the given data of the code above?
Probably you can access those headers via the os.environ dictionary.