DEV Community

Sai Avinash Duddupudi
Sai Avinash Duddupudi

Posted on

POST request in redux-saga not working

I would like to make post request in redux-saga. My code looks like:

var formdata = new FormData();
    formdata.append("points", "a,b");
    formdata.append("pid", "someid");
    formdata.append("tions", "cses");
    formdata.append("exp", "cx");
const ans = yield call(fetch,
'http://localhost:8000/api',
{
    method: "POST",
    headers: { 'Authorization': 'Token tokenid',
                "Content-Type": "application/json",},
    body : formdata
});
Enter fullscreen mode Exit fullscreen mode

In Postman it is working fine(selected form-data in body) but in saga, the POST request is giving error as the server responded with a status of 400 (Bad Request)

Please suggest where I am doing wrong as the API is working fine and all the parameters are correct?

Top comments (2)

Collapse
 
saiavinashiitr profile image
Sai Avinash Duddupudi • Edited

Update :- The 400 Bad request has gone away after I added "Content-Type": "application/x-www-form-urlencoded" in the headers and changed the body type accordingly

Example :-

fetch("api/xxx", {
    body: "email=test@example.com&password=pw",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "post",
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kriscannabis profile image
Pavel Slobozhaninov

body : JSON.stringify(formdata)