psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.2k stars 9.34k forks source link

Issue With Decimals In JSON Handling #6686

Closed Hady-Eslam closed 7 months ago

Hady-Eslam commented 7 months ago

Issue In Handling Decimal in JSON when simplejson package is not installed

Expected Result

When There is a Decimal in JSON data it should be handled without an error

import requests
from decimal import Decimal

data = {
    'key': Decimal('0.0')
}

print(requests.post('https://jsonplaceholder.typicode.com/', json=data))

It should send the request without an issue

Actual Result

When installing the requests Package

pip3 install requests==2.31.0

and running this script

import requests
from decimal import Decimal

data = {
    'key': Decimal('0.0')
}

print(requests.post('https://jsonplaceholder.typicode.com/', json=data))

it gives an exception

TypeError: Object of type Decimal is not JSON serializable

But When installing SimpleJson With It

pip3 install requests==2.31.0 simplejson==3.17.2

and running the same script, it will send requests normally without an issue

Reproduction Steps

install the requests package and run the bellow script, and then install simplejson package and run the same script, You will see different results

import requests
from decimal import Decimal

data = {
    'key': Decimal('0.0')
}

print(requests.post('https://jsonplaceholder.typicode.com/', json=data))

System Information

$ python3 -m requests.help
{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "2.1.1"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.10.12"
  },
  "platform": {
    "release": "6.5.0-26-generic",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.31.0"
  },
  "system_ssl": {
    "version": "30000020"
  },
  "urllib3": {
    "version": "2.0.7"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}
mohamedynawar commented 7 months ago

I was able to reproduce this on Mac OS. System information:

{
  "chardet": {
    "version": null
  },
  "charset_normalizer": {
    "version": "3.3.1"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.11.5"
  },
  "platform": {
    "release": "23.3.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.31.0"
  },
  "system_ssl": {
    "version": "30100020"
  },
  "urllib3": {
    "version": "2.0.7"
  },
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}
Hossam-Ameen commented 7 months ago

I have the same issue. System information: { "chardet": { "version": "4.0.0" }, "charset_normalizer": { "version": "2.0.6" }, "cryptography": { "version": "" }, "idna": { "version": "3.3" }, "implementation": { "name": "CPython", "version": "3.10.12" }, "platform": { "release": "6.5.0-27-generic", "system": "Linux" }, "pyOpenSSL": { "openssl_version": "", "version": null }, "requests": { "version": "2.31.0" }, "system_ssl": { "version": "30000020" }, "urllib3": { "version": "2.0.7" }, "using_charset_normalizer": false, "using_pyopenssl": false }

sigmavirus24 commented 7 months ago

This is the behavior of the standard library. We do not customize the encoder or decoder but you can do that yourself. Or you can use simplejson