Search code examples
pythonjsonrestpostman

How can I use the term None instead of null in post request python project?


In python, I had this variable:

v = None 

and this post request:

issue = {
                    "fields": 
                        {
                            "project": 
                            {
                                "id": 'x'
                            },
                            "summary": 's',
                            "issuetype": 
                            {
                                "name": 'n'
                            },
                            "components": 
                            [
                                {
                                "name": 'c1'
                                },
                                {
                                "name": v
                                },
                            ]
                         }

After debugging I got this error message:

'{"errorMessages":[],"errors":{"components":"Component name 'None' is not valid"}}'

The data is dynamic and that v as well, in case it is entered as a string then that won't be no issue at all but it if is empty in the entered data I want to skip in the post request then that will be an issue.

In postman I had no issue I add null to it and then it works.

It is just that in python where I cannot find a way to add that null. hence I tried None, 'null' and no way.

Isn't there any solution for this?


Solution

  • Resolved:

                #convert dict to string
                r = json.dumps(issue)
                 
                #Replace None by null in the string
                r = r.replace("\"None\"", "null")
                
                #convert back the string to dict
                loaded_r = json.loads(r)
    
                response_post = requests.post(
                url_post,
                headers=headers , 
                json =  loaded_r 
                )