IngridGdesigns / Urban_Garden

Urban Garden is a food bartering app that reduces the impact of food waste, builds community, and creates an additional digital farmers’ barter market.
2 stars 2 forks source link

Get post to work #2

Closed IngridGdesigns closed 5 years ago

IngridGdesigns commented 5 years ago

get post to work in React

IngridGdesigns commented 5 years ago

Need to work on delete users, user items and edit users, user items...

IngridGdesigns commented 5 years ago

Finally got it to work, forgot to add the 'Content-Type': 'application/json' in the headers with the auth token. Like so : const headers = { 'Authorization': Bearer ${this.props.auth.accessToken}, 'Content-Type': 'application/json'}

let data = {
                item_name: document.getElementById('itemInput').value,

    }

    fetch('http://localhost:3009/user_items', {
        method: 'POST',
        headers: headers,
        body: JSON.stringify(data),
    })
    .then(res => res.json())
    .then(response => this.setState({ user_items: [...this.state.user_items, response]}))
}

https://stackoverflow.com/questions/24543847/req-body-empty-on-posts