axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

POST request with params?

Open OliverEvans96 opened this issue 4 years ago • 1 comments

Hi there,

Thanks for the great package!

Just a quick question - it seems that it's not currently possible to .reply or .replyOnce to a POST request containing specific URL parameters due to the definition of var allowedParamsMethods = ["delete", "get", "head", "options"]; here

Is it a general rule that only these HTTP methods should have URL parameters? Is there some other reason for not allowing url parameters in all verbs here? Am I misunderstanding?

Thanks a lot! Oliver

OliverEvans96 avatar Apr 13 '20 15:04 OliverEvans96

I ran into this issue as well -- I ended up forking the repository and making a change to the isBodyOrParametersMatching utility function: https://github.com/KindHealth-Holdings/axios-mock-adapter/commit/6793904f9e6a031a4e3bfa439025b08500d1cc8d

I haven't battle tested it much just yet and I'm not 100% satisfied with the implementation, but here's how I've been using it with a POST request matcher using the params key of the body:

httpMock.onPost(
  'https://host.example/path', 
  {key: 'value', other: 'value', params: {key: 'value'}}
).replyOnce(200, {response: {key: 'value'}})

If you want to try out my fork, you can just do:

yarn add -D https://github.com/KindHealth-Holdings/axios-mock-adapter.git#match-params-on-post

I'll likely be making changes there, so I can't guarantee the stability of that branch, but it may help you get unblocked.

reagent avatar Mar 09 '21 17:03 reagent