0

I have a str payload looks like this

payload = "{\"fqdn\":\"examplazdazdazazdzadza.com\",\"duration\":5,\"owner\":{\"city\":\"Paris\",\"given\":\"Alice\",\"family\":\"Doe\",\"zip\":\"75001\",\"country\":\"FR\",\"streetaddr\":\"5 rue neuve\",\"phone\":\"+33.123456789\",\"type\":0,\"email\":\"alice@example.org\"}}"

This is the payload from the Gandi API

I want to make the payload a bit more dynamic and have some flexibility, so I tired dict

domain = 'example.com`

        payload = {
            'fqdn': domain,
            'duration': 1,
            'owner': {
                "city": "Paris",
                "given": "Alice",
                "family": "Doe",
                "zip": "75001",
                "country": "FR",
                "streetaddr": "5 rue neuve",
                "phone": "+33.123456789",
                "state": "FR-J",
                "type": 0,
                "email": "alice@example.org"
            }
        }

After this I need to revert back to the orginal datetype (str) and I do this like so

payload = '\n'.join('\%s\: "\%s\"' % (k, v) for k, v in payload.items())

However this returns

Bad Request

.

Any ideas how get this done properly?

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
user3641381
  • 1,036
  • 3
  • 15
  • 29
  • 1
    As the linked doc tells you, this is JSON, use the tools in the [json](https://docs.python.org/3/library/json.html) module to serialise and deserialise (or use tools like [requests](https://2.python-requests.org/en/master/) that will do it automatically) – snakecharmerb May 12 '20 at 09:11

1 Answers1

6

Can do this using the JSON Module:

In [409]: import json                                                                                                                                                                                       

In [410]: json.dumps(payload)                                                                                                                                                                               
Out[410]: '{"fqdn": "domain", "duration": 1, "owner": {"city": "Paris", "given": "Alice", "family": "Doe", "zip": "75001", "country": "FR", "streetaddr": "5 rue neuve", "phone": "+33.123456789", "state": "FR-J", "type": 0, "email": "alice@example.org"}}'

After OP's comments:

In [411]: domain = 'example.com'                                                                                                                                                                            

In [412]: payload = { 
     ...:             'fqdn': domain, 
     ...:             'duration': 1, 
     ...:             'owner': { 
     ...:                 "city": "Paris", 
     ...:                 "given": "Alice", 
     ...:                 "family": "Doe", 
     ...:                 "zip": "75001", 
     ...:                 "country": "FR", 
     ...:                 "streetaddr": "5 rue neuve", 
     ...:                 "phone": "+33.123456789", 
     ...:                 "state": "FR-J", 
     ...:                 "type": 0, 
     ...:                 "email": "alice@example.org" 
     ...:             } 
     ...:         }                                                                                                                                                                                         

In [413]: json.dumps(payload)                                                                                                                                                                               
Out[413]: '{"fqdn": "example.com", "duration": 1, "owner": {"city": "Paris", "given": "Alice", "family": "Doe", "zip": "75001", "country": "FR", "streetaddr": "5 rue neuve", "phone": "+33.123456789", "state": "FR-J", "type": 0, "email": "alice@example.org"}}'
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • This would work with Python3 only. Python2 will probably mess up with the quotes. – Prateek Dewan May 12 '20 at 09:11
  • 2
    @PrateekDewan: Why would Python2 mess up the quotes? Can you give an example of such a scenario, or a link describing it? The only problem with this answer is if you're on legacy systems with pre-2.6 Python, as it didn't have native support for JSON. – Amadan May 12 '20 at 09:15
  • The quotes are escaped. I tried with a Python2 interpreter, and this is what it produced. The output is still a string. '"{\\"fqdn\\":\\"examplazdazdazazdzadza.com\\",\\"duration\\":5,\\"owner\\":{\\"city\\":\\"Paris\\",\\"given\\":\\"Alice\\",\\"family\\":\\"Doe\\",\\"zip\\":\\"75001\\",\\"country\\":\\"FR\\",\\"streetaddr\\":\\"5 rue neuve\\",\\"phone\\":\\"+33.123456789\\",\\"type\\":0,\\"email\\":\\"alice@example.org\\"}}"' – Prateek Dewan May 12 '20 at 09:23
  • @Mayank Porwal How can I pass a the domain param after dump? – user3641381 May 12 '20 at 09:37
  • @user3641381 Check 2nd part of my updated answer. – Mayank Porwal May 12 '20 at 09:39