Search code examples
pythonpython-requestsquery-stringurlencode

Python requests - correct way to send query-string for POST


Here is what I want to be sent:

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

Body:
{
<JSON>
}

Here is how I send it.

Why I didn't use a dictionary for params

Because when I did, the @ in the email and the ! in the password got encoded.

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

What I got

{
    "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"
}

How can I fully debug what really got sent?

What I did find was:

l_response.request.body

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

My conclusion is that I am definitely not sending the query-string correctly.

What am I doing wrong?

What else I tried

It goes all wrong if I set the url argument to https://dev.xxx.com/employee-service/login?email=apitester@xxx.com&password=xxx!xxx: many errors, re-tries, SSL exceptions, etc.


Solution

  • As commented, this worked.

    I will close this item.

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