skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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 
                )
    

  2. It looks like the post request is json format. The object you have is a python object; what you need is the json-formatted string equivalent:

    import json
    
    type(issue) # dict
    json_payload = json.dumps(issue, indent=2)
    type(json_payload) # string
    
    print(json_payload)
    # {
    #   "fields": {
    #     "project": {
    #       "id": "x"
    #     },
    #     "summary": "s",
    #     "issuetype": {
    #       "name": "n"
    #     },
    #     "components": [
    #       {
    #         "name": "c1"
    #       },
    #       {
    #         "name": null
    #       }
    #     ]
    #   }
    # }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search