Is it possible to use moxios to mock a reply to a POST request that will match not just by URL but also by the POST body? Inspecting the body afterwards would work for me too.
This is what I am doing now. As far as I know there are no method specific stubbing methods:
describe('createCode', function () {
it('should create new code', function () {
moxios.stubRequest(process.env.API_URL + '/games/GM01/codes', {
status: 200
})
})
})
POST Requests with Axios Sep 17, 2019 The easiest way to make a POST request with Axios is the axios.post () function. The first parameter to axios.post () is the URL, and the 2nd is the HTTP request body.
Axios offers methods for all the HTTP verbs, which are less popular but still used: and a method to get the HTTP headers of a request, discarding the body: Another really powerful feature of Axios that cannot be understated is the ability to execute multiple requests in parallel, simply provide an array argument to axios.all.
Run the following command to install the axios with npm or yarn CLI. 2. POST request with a JSON body using axios Let’s use the following syntax for the POST request. We have used the post method of the axios and attached the JSON body with the request. 3. POST request with HTTP header We can use the third parameter to pass the HTTP headers.
The first parameter to axios.post () is the URL, and the 2nd is the HTTP request body. By default, if the 2nd parameter to axios.post () is an object, Axios serializes the object to JSON using the JSON.stringify () function .
There is a way to inspect the last axios request using moxios:
let request = moxios.requests.mostRecent()
expect(request.config.method).to.equal('post')
expect(JSON.parse(request.config.data)).to.deep.equal(code)
The config object is what's being passed in axios, data
is the body of the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With