0

Here is a small quote from this answer:

import requests
import json

data = {"data" : "24.3"}
data_json = json.dumps(data)
headers = {'Content-type': 'application/json'}

response = requests.post(url, data=data_json, headers=headers)

Does anyone know for sure whether it matters whether you have

data = {"data" : "24.3"}

or

data = {"data" : 24.3} ?

cardamom
  • 6,873
  • 11
  • 48
  • 102

2 Answers2

5

You are already giving a string to requests.post(), because you convert your dictionary to a JSON document with json.dumps(). It doesn't matter to requests what this string contains.

It only matters to whatever server you are sending this data; it is that server that will decode the JSON document and use your number or string.

Note that requests can do the JSON conversion for you. There is no need to use json.dumps() here, just pass your dictionary to the json keyword argumnet:

import requests

data = {"data" : "24.3"}

response = requests.post(url, json=data)

This also takes care of setting the Content-Type header to application/json.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

There are two unrelated questions in your post.

The first is:

Does anyone know for sure whether it matters whether you have

data = {"data" : "24.3"}

or

data = {"data" : 24.3} ?

Yes, it does matter!
They are completely different things.
Treating them the same would make JSON format usage obsolete.

If server expects key "data" to be JSON data type number and you send it as a JSON data type string instead, a HTTP status code 400 should be responded.

If server does not report any error it means that this particular key is not being used in server and/or it is not being validated in server.

If server does treat them the same it is idiotic rather than redundant. That is why JSON format is being used in the first place.

The second is:

Does Python Requests POST need numerical data to be a string rather than a float?

This question title is explained in Martijn Pieters's answer.

Elis Byberi
  • 1,422
  • 1
  • 11
  • 20