英文:
Python GET request not matching CURL output
问题
The main difference between the Python code and the CURL request is in how the data is sent in the GET request. In the CURL request, the parameters are included in the URL itself, whereas in the Python code, you are sending them as data in the request body.
In CURL:
curl -X GET -H "Content-Type: application/x-www-form-urlencoded" "https://sim.logonvalidation.net/authorize?response_type=code&client_id=37656c896c424f8cab64774e603bc927&state=y90dsygas98dygoidsahf8sa&redirect_uri=http%3A%2F%2Flocalhost%2FmyAppTest"
In Python:
data = {'response_type': 'code',
'client_id': config['client_id'],
'state': 'y90dsygas98dygoidsahf8sa',
'redirect_uri': config['url']}
response = requests.request('GET', url=endpoint, data=data, headers=head)
To make the Python code behave more like the CURL request, you should include the parameters directly in the URL when making the GET request, like this:
url = f"{endpoint}?response_type=code&client_id={config['client_id']}&state=y90dsygas98dygoidsahf8sa&redirect_uri={config['url']}"
response = requests.request('GET', url=url, headers=head)
This way, the parameters will be part of the URL, just like in the CURL request.
英文:
I am working with the SAXO OpenAPI. I need to authenticate using some non-confidential credentials as a first step of the authentication flow.
The following CURL request works fine and returns the response that I expect
curl -X GET -H “Content-Type: application/x-www-form-urlencoded” "https://sim.logonvalidation.net/authorize?response_type=code&client_id=37656c896c424f8cab64774e603bc927&state=y90dsygas98dygoidsahf8sa&redirect_uri=http%3A%2F%2Flocalhost%2FmyAppTest"
However, when I try to do the same request in python, I get a status code 400 signaling an error. This is my python code
import requests
def authenticate(config: dict = None):
if config is None:
return False
endpoint = config['AuthorizationEndpoint']
head = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {'response_type': 'code',
'client_id': config['client_id'],
'state': 'y90dsygas98dygoidsahf8sa',
'redirect_uri': config['url']}
response = requests.request('GET', url=endpoint, data=data, headers=head)
print(response)
config = {'AuthorizationEndpoint': 'https://sim.logonvalidation.net/authorize',
'client_id': '37656c896c424f8cab64774e603bc927',
'url': 'http://localhost/myAppTest'}
authenticate(config)
I thought that the python code should have done the same request as in CURL, but apparently something is different. Can you tell me in what they differ?
答案1
得分: 2
application/x-www-form-urlencoded
请求在URL中直接编码数据。您将数据放在字典中,并将其像application/json
请求一样发布到请求正文中。
用curl的URL替换端点URL应该可以解决问题。
import requests
def authenticate(config: dict = None):
if config is None:
return False
endpoint = config['AuthorizationEndpoint']
head = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.request('GET', url=endpoint, headers=head)
print(response)
config = {'AuthorizationEndpoint': '"https://sim.logonvalidation.net/authorize?response_type=code&client_id=37656c896c424f8cab64774e603bc927&state=y90dsygas98dygoidsahf8sa&redirect_uri=http%3A%2F%2Flocalhost%2FmyAppTest"',
'client_id': '37656c896c424f8cab64774e603bc927',
'url': 'http://localhost/myAppTest'}
authenticate(config)
英文:
The application/x-www-form-urlencoded
requests have the data encoded right in the URL. You put the data inside a dictionary and is posting it in the body like an application/json
request.
Replacing the endpoint URL with the URL of the curl should do the trick.
import requests
def authenticate(config: dict = None):
if config is None:
return False
endpoint = config['AuthorizationEndpoint']
head = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.request('GET', url=endpoint, headers=head)
print(response)
config = {'AuthorizationEndpoint': '"https://sim.logonvalidation.net/authorize?response_type=code&client_id=37656c896c424f8cab64774e603bc927&state=y90dsygas98dygoidsahf8sa&redirect_uri=http%3A%2F%2Flocalhost%2FmyAppTest"',
'client_id': '37656c896c424f8cab64774e603bc927',
'url': 'http://localhost/myAppTest'}
authenticate(config)
答案2
得分: 0
看起来在你的Python代码中,你将数据作为请求体发送,但你应该将它作为查询参数发送。尝试这样做:
response = requests.request('GET', url=endpoint, params=data, headers=head)
或者
response = requests.get(url=endpoint, params=data, headers=head)
英文:
It seems like in your python code you are sending data as a body of a request, but you should send it as a query params. Try this:
response = requests.request('GET', url=endpoint, params=data, headers=head)
or
response = requests.get(url=endpoint, params=data, headers=head)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论