Home > Mobile >  Sending raw data with Axios
Sending raw data with Axios

Time:08-20

I am testing a server implementation and need to send invalid data to the server. When using axios post with Content-Type application/json the data gets automatically parsed as JSON. If the data axios receives is invalid JSON it will automatically turn this into valid JSON by quoting the data as a string.

axios.post('api/paths/invalid.json', '{"invalid: ()',
  { headers: { 'Content-Type': 'application/json' } })

The above will actually send "{\"invalid: ()" to the server, which will parse as valid JSON.

CodePudding user response:

Override the default request transformation to be able to send any raw unmodified data to the server with data => data (AKA the id-function).

axios.post('api/paths/invalid.json', '{"invalid: ()',
  { 
    headers: { 'Content-Type': 'application/json' },
    transformRequest: [data => data]
  }
)
  • Related