Values become null if I leave them empty in PUT

My question:

Values become null if I leave them empty in PUT

Hello!

For the most part, let’s say I have a parameter like:

{

        "_id": 31,

        "username": "userName2",

        "email": "test@Gmail.com",

        "password": "password",

        "gender": "Female",

        "register_date": "2022-01-04",

        "about": "A very cool user",

        "fav_received": 0,

        "fav_given": 0,

        "profile_picture": "images/pfp3",

        "member_type": "Normal",

        "reviews_made": 0

    }

And I just want to update its username via PUT to something like

{

        "username": "New Username"

}

I want a result like:

{

        "_id": 31,

        "username": "New Username",

        "email": "test@Gmail.com",

        "password": "password",

        "gender": "Female",

        "register_date": "2022-01-04",

        "about": "A very cool user",

        "fav_received": 0,

        "fav_given": 0,

        "profile_picture": "images/pfp3",

        "member_type": "Normal",

        "reviews_made": 0

    }

With all the other values intact,

But I get:

{

        "_id": 31,

        "username": "New Username",

        "email": null,

        "password": null,

        "gender": null,

        "register_date": null,

        "about": null,

        "fav_received": null,

        "fav_given": null,

        "profile_picture": null,

        "member_type": null,

        "reviews_made": null

    }

All the other values I did not update in the previous post request become null. How would I be able to fix this? Thank you in advance!

@lumine27 Welcome to the community :partying_face:

Wishing you a happie new year!!

Did you checked the API documentation on the sample request body of the PUT request? Usually PUT will have only the tags to be updated, but for some APIs you need to provide the entire body along with the field name which needs to be updated. Did you tried that way?

1 Like

Hi @lumine27,

Generally, the HTTP PUT method is used to update, which means it replaces the content of the resource as provided by the request body.

The operation which you are trying is PATCH, and there is an HTTP PATCH method also available, which will just update one or 2 fields as requested.

I totally agree with @bpricilla, you may need to check the API documentation for a better understanding and how actually API is configured at the back end. :slight_smile:

1 Like