0

I need to request post a 36-digit id with other things to a url. This code should be sent as json. But when I try to json.dumps() it, the initial and final quotes are also counted and as you can see I reach 38 characters and get error.

merchant_id='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
merchand_id_json = json.dumps(merchant_id)

#check len of merchat id and merchant id in json format
print('len merchant_id is', len(merchant_id))
print('len merchand_id_json is', len(merchand_id_json))

# Set header 
request_header = {
    'accept': 'application/json',
    'content_type': 'application/json',
}

# Send request
res = requests.post(
        url=request_url, 
        data=
        {
            "merchant_id": merchand_id_json,
        }, 
            headers=request_header)

# Check res content in terminal 
print('res =', res) 

# Check res.text in terminal
print('res.text =', res.text)

in Terminal:

len merchant_id befor json is 36
len merchand_id after json is 38
res =  <Response [200]>
res.text = {"data":[],"errors":{"code":-9,"message":"The input params invalid, validation error.","validations":[{"merchant_id":"The merchant id may not be greater than 36 characters."},{"merchant_id":"string is not a valid uuid."}]}}

How can I convert this 36-digit id to json format without increasing the number of characters?

  • `json.dump()` requires two arguments, so calling `json.dump(merchant_id)` is an obvious error. This cannot be your real code. Please show us the real code. – John Gordon Sep 02 '23 at 23:19
  • 1
    @JohnGordon I assume it's a typo considering the title specifically calls out `dumps` – Aaron Sep 02 '23 at 23:20
  • Strings in Json must have quotes. Is the ID supposed to be a number instead? – Aaron Sep 02 '23 at 23:22
  • it is json.dumps. edited now – cosmoscoder Sep 02 '23 at 23:22
  • 1
    A string is valid json. Have you tried just passing the string in? `"merchant_id": merchand_id`? – Mark Sep 02 '23 at 23:22
  • [this](https://stackoverflow.com/a/61839372/3220135) seems like the answer. UUID is evidently a native JSON data type not requiring quotes, but python is just treating it as a string – Aaron Sep 02 '23 at 23:25
  • yes. I just entered the code and quoted to make it json. @ – cosmoscoder Sep 02 '23 at 23:25
  • Because `data` is a `dict`, `request.post` converted it to a form that included an already-json-encoded string. I think you want to skip doing the json encoding youself, stop using data= and instead add the parameter `json={"merchant_id": merchand_id}`. requests will json encode and add content-type to json. – tdelaney Sep 02 '23 at 23:32
  • If the site really did want a form, it would be `data={"merchant_id": merchant_id}` – tdelaney Sep 02 '23 at 23:33

2 Answers2

0

You don't need to call json.dumps() at all. Requests will convert the outgoing data to json automatically, if you use the json= argument instead of data=.

res = requests.post(
    url=request_url, 
    json={"merchant_id": merchant_id},
    headers=request_header)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

The quotes are added by json.dumps() automatically.

To avoid the quotes, you can just pass the merchant_id string directly in the request body without json.dumps():

data = {
  "merchant_id": merchant_id
}

requests.post(url, data=data) 

Now the request body will contain the raw merchant_id string without any quotes added.

By passing the string directly instead of encoding to JSON, it avoids the extra quotes added by json.dumps(). This prevents increasing the character length.

pwoltschk
  • 550
  • 1
  • 3
  • 22