【问题标题】:Python requests - correct way to send query-string for POSTPython 请求 - 为 POST 发送查询字符串的正确方法
【发布时间】:2022-06-19 17:26:15
【问题描述】:

这是我想要发送的内容:

https://dev.xxx.com/employee-service/login?email=apitester@xxx.com&password=xxx!xxx

Body:
{
<JSON>
}

这是我发送它的方式。

为什么我没有为参数使用字典

因为当我这样做时,电子邮件中的 @ 和 !在密码被编码。

    params = 'email=' + l_email + '&password=' + l_password
    l_response = requests.post(url=url, data=params, json=body, headers={'Content-Type': 'text/plain'}, verify=False)

我得到了什么

{
    "timestamp": "2022-05-16T12:20:56.918+0000",
    "status": 400,
    "error": "Bad Request",
    "errors": [
        {
            "codes": [
                "NotEmpty.systemUserLogin.password",
                "NotEmpty.password",
                "NotEmpty.java.lang.String",
                "NotEmpty"
            ],
            "arguments": [
                {
                    "codes": [
                        "systemUserLogin.password",
                        "password"
                    ],
                    "arguments": null,
                    "defaultMessage": "password",
                    "code": "password"
                }
            ],
            "defaultMessage": "must not be empty",
            "objectName": "systemUserLogin",
            "field": "password",
            "rejectedValue": null,
            "bindingFailure": false,
            "code": "NotEmpty"
        },
        {
            "codes": [
                "NotNull.systemUserLogin.password",
                "NotNull.password",
                "NotNull.java.lang.String",
                "NotNull"
            ],
            "arguments": [
                {
                    "codes": [
                        "systemUserLogin.password",
                        "password"
                    ],
                    "arguments": null,
                    "defaultMessage": "password",
                    "code": "password"
                }
            ],
            "defaultMessage": "must not be null",
            "objectName": "systemUserLogin",
            "field": "password",
            "rejectedValue": null,
            "bindingFailure": false,
            "code": "NotNull"
        },
        {
            "codes": [
                "NotNull.systemUserLogin.email",
                "NotNull.email",
                "NotNull.java.lang.String",
                "NotNull"
            ],
            "arguments": [
                {
                    "codes": [
                        "systemUserLogin.email",
                        "email"
                    ],
                    "arguments": null,
                    "defaultMessage": "email",
                    "code": "email"
                }
            ],
            "defaultMessage": "must not be null",
            "objectName": "systemUserLogin",
            "field": "email",
            "rejectedValue": null,
            "bindingFailure": false,
            "code": "NotNull"
        },
        {
            "codes": [
                "NotEmpty.systemUserLogin.email",
                "NotEmpty.email",
                "NotEmpty.java.lang.String",
                "NotEmpty"
            ],
            "arguments": [
                {
                    "codes": [
                        "systemUserLogin.email",
                        "email"
                    ],
                    "arguments": null,
                    "defaultMessage": "email",
                    "code": "email"
                }
            ],
            "defaultMessage": "must not be empty",
            "objectName": "systemUserLogin",
            "field": "email",
            "rejectedValue": null,
            "bindingFailure": false,
            "code": "NotEmpty"
        }
    ],
    "message": "Validation failed for object='systemUserLogin'. Error count: 4",
    "path": "/employee-service/login"
}

我如何才能完全调试真正发送的内容?

我发现的是:

l_response.request.body

# email=apitester@xxx.com&password=xxx!xxx

我的结论是我肯定没有正确发送查询字符串。

我做错了什么?

我还尝试了什么

如果我将url 参数设置为https://dev.xxx.com/employee-service/login?email=apitester@xxx.com&amp;password=xxx!xxx,就会出错:许多错误、重试、SSL 异常等。

【问题讨论】:

  • 我不明白的地方:您想以 JSON 格式发送您的 POST 数据吗?在这种情况下,您可以这样发送请求: l_response = requests.post(url=url, data=json.dumps(YOUR_DICT), headers={'Content-Type': 'application/json'}, verify=False)否则,参数由 & 号分隔的格式为 Content-Type "application/x-www-form-urlencoded"。那么也许你对“text/plain”的使用是错误的?
  • 这里有一些确实有效的方法:` # data={'locationId': location_id} l_response = s.post(url=url+'?locationId=110', json=user_json, verify=False, headers={'Content-Type': 'application/json'})` 我不喜欢它,因为你应该为data 使用dict 参数。但它仍然

标签: python python-requests query-string urlencode


【解决方案1】:

正如评论的那样,这行得通。

我会关闭这个项目。

l_response = s.post(url=url+'?locationId=110', json=user_json, verify=False, headers={'Content-Type': 'application/json'})

【讨论】:

    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2016-03-25
    • 1970-01-01
    相关资源
    最近更新 更多