Sending a list of object as body in HTTP request in React Native

Viewed 1204

I am trying to make an Http call which requires a list of object as body parameter in POST API. List of objects are as follow with the name joinedGroupList.

[{"groupId":10,"joinIndicator":true},
{"groupId":13,"joinIndicator":true},
{"groupId":1,"joinIndicator":true}]

My code looks something like this

    try {
        const response = await fetch(saveJoinedGroups(), {
            method: 'POST',
            body: JSON.stringify(joinedGroupsList),
            headers: {
                'Content-Type': 'application/json'
              },
        });
        const responseJson = await response.json();
        console.log("responseJson is ",responseJson);

        return responseJson;
    }
    catch (error) {
        console.error("error is ",error);
        return { err: error };
    }

I get an error from my server about bad request. Obviously I believe that I am making a mistake in sending a list of object. Not too mention that I am new to React Native

1 Answers

Maybe the server accepts the body as an object instead of a string. Try removing JSON.Stringify from the body and make it like this: body: joinedGroupsList

Related