-2

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?

  • Does this answer your question? [Python: most idiomatic way to convert None to empty string?](https://stackoverflow.com/questions/1034573/python-most-idiomatic-way-to-convert-none-to-empty-string) – esqew Aug 14 '23 at 16:03
  • 1
    Since you haven't provided a [mre], it's not clear how you're actually using `issue` to make your actual request, but if you're generating JSON you should use a serializer that transparently converts Python's `None` to JSON's `null`. For instance, `json.dumps(None) # == 'null'` – esqew Aug 14 '23 at 16:05
  • I provided only some snippets, it is just that the v variable is sometimes entered from a csv as a text which is good, but sometimes it is empty, and the API client doesn't accept it as empty since it is mandatory, it is accepted only as null, and that null I cannot assign it to that v in python so I assigned the None that doesn't work – SeifeddineBMZ Aug 14 '23 at 16:12

1 Answers1

0

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 
            )