-4
data = f'\u007b"domainNames":{domain_list}\u007d'
response = requests.post(
    'https://api.name.com/v4/domains:checkAvailability',
    headers=headers, data=data, auth=auth)

The problem is that I'm trying to send a list in 'domain_list'. But it shows the following error:

{'message': 'Invalid Argument', 'details': 'Error occurred during parsing: Cannot decode json string.'}

when the strings inside the list are single-quoted (example: ['example1.com', 'example2.com']). But if I post the request with double-quoted strings (i.e. ["example1.com", "example2.com"]) I get the results fine. Since I am loading the domain names from a file, is there a way to get double-quoted strings instead of single-quoted ones?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 3
    The endpoint almost certainly expects JSON, in which case you should use `json={'domainNames': domain_list}` so that your list is properly encoded. – chepner Sep 20 '21 at 21:54
  • 1
    `requests` has a field for `json` (instead of `data`), which should auto format for you. Also, why not format your data as a Python dictionary? You shouldn't need to do any manual encoding. – h0r53 Sep 20 '21 at 21:54
  • If you don't know what JSON is, you should go look that up. Python string formatting is not intended to produce JSON, and there are more differences than just quotation marks. Python has a `json` module for making JSON, though as others have said, `requests` can handle that for you. – user2357112 Sep 20 '21 at 21:56
  • The `data` param generally accepts a Python object not a string. so if you really, really wanted to it might work like `data=eval(data)`. – rv.kvetch Sep 20 '21 at 22:12
  • @Safwan Amin, I edited my answer. Can you check it? It was wrong. – HASAN HÜSEYİN YÜCEL Sep 22 '21 at 20:24

1 Answers1

-1

You can send the list to name.api with the code which is below.

Code:

import requests, json

headers = {
    'Content-Type': 'application/json',
}
domains = ["test.com","hasanyucel.com","hasan.com"]
data = json.dumps({"domainNames": domains})
headers = {'Content-Type': 'application/json',}
response = requests.post('https://api.name.com/v4/domains:checkAvailability', headers=headers, data=data, auth=('username', 'token')).content.decode()
response = json.loads(response)
domain_list = response["results"]