Python GET请求与CURL输出不匹配

huangapple go评论78阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年6月8日 23:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定