简体   繁体   中英

Redux Thunk Dispatch Axios Post Request Body

So I am kinda stuck in trying to send a request body in Redux Thunk using axios . The app I work on has a particular setup, and we have a service which exposes backend methods that we can use in a dispatch call, like so:

// action file

export const subscribeUser = (id, token) => async dispatch => {
  await dispatch({
    type: t.SOME_ACTION_FETCHING,
    api: 'nameOfApi',
    method: 'createSubscription',
    payload: {
      userId: id
    }
  }).then(
    response => dispatch({ type: t.SOME_ACTION_FETCHED, subscriptionStatus: response })
  );
};

Where nameOfApi is the backend to communicate with, and createSubscription is the appropriate post method. What I can't seem to get working is sending a request body in this request.

In the dispatch logic, I have tried

await dispatch({
  type: t.SOME_ACTION_FETCHING,
  api: 'nameOfApi',
  method: 'createSubscription',
  payload: {
    userId: id,
    subscribeToken: token
  }
})

and

await dispatch({
  type: t.SOME_ACTION_FETCHING,
  api: 'nameOfApi',
  method: 'createSubscription',
  payload: {
    userId: id,
  },
  subscribeToken: token
})

So the route I am trying to hit is https://gateway.dev.url/nameOfApi/{userId}/subscription . And I am trying to send token in the request body.

The following route successfully returns the response I would need when I use Postman to ping https://gateway.dev.url/nameOfApi/1234/subscription ( with the appropriate headers, which are already handled by a service ), and in the Body tab in Postman, I send

{
  "subscribeToken": "abcd1234"
}

which is what the API requires. But in my React/Redux code, I am getting either 500 errors, or 409 errors because the entire request doesn't match the Swagger validation, which the latter proves pinging the API is working, the request is just being sent in the wrong way.

Can anyone please point me in the right direction of how to send subscribeToken in the request body of a post request with axios with Redux Thunk dispatch ?

Thank you in advance for any potential guidance...!

EDIT : And to add, there is also a get request which expects only userId as a URL param, and the below resolves and returns a successful response:

await dispatch({
  type: t.SOME_ACTION_FETCHING,
  api: 'nameOfApi',
  method: 'getSubscription',
  payload: {
    userId: id,
  }
})

So the issue with the post is definitely just a syntax problem, in how I would transcribe what works in Postman ( the Body tab parameter ), into this dispatch logic.

It actually looks like I was missing a wrapper parameter from the API, and the dispatch request needed to look like

await dispatch({
  type: t.SOME_ACTION_FETCHING,
  api: 'nameOfApi',
  method: 'createSubscription',
  payload: {
    userId: id,
    newToken: {
      subscribeToken: token
    }
  }
})

So looks I'm all sorted now. Mods can close this question, unless it's informative to the community to keep around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM