Reputation: 61
I'm just curious on how should I use delete in superagent, its hard to search for any delete superagent documentation I am currently using asp.net as my API and reactjs as front end I have this button:
<td><center><button className ="btn btn-danger" onClick={this.deleteEmployee.bind(this, d.Employee_ID)}>Delete</button></center></td>
How will I connect it on superagent using fetch? I'm using a localhost:
http://localhost:5118/api/employeedetails/DeleteEmployeeDetail/
Upvotes: 0
Views: 3228
Reputation: 4075
You may get a 405 error if you directly use a delete request.
For instance, you may have an endpoint like this one
Route::middleware('auth:api')->delete('/orders/{id}', 'ApiController@destroyOrder');
and you make a delete request using superagent like below
superagent
.del(`${process.env.BASE_URL}/api/orders/${params.id}`)
.send(body)
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${process.env.API_TOKEN_SECRET}`)
.then(response => {})
.catch(error => {})
and got a response like this: "GET method is not supported for this route"
To overcome this issue, here's an alternative:
superagent
.post(`${process.env.BASE_URL}/api/orders/${params.id}`)
.send({ _method: "DELETE" })
.set('Content-Type', 'application/json')
.set('Authorization', `Bearer ${process.env.API_TOKEN_SECRET}`)
.then(response => {})
.catch(error => {})
Upvotes: 0
Reputation: 1671
Use del()
see the documentation
DELETE can be also called as .del() for compatibility with old IE where delete is a reserved word.
npm install superagent
var request = require('superagent');
deleteEmployee(id) {
request
.del('/api/employeedetails/DeleteEmployeeDetail')
.send({ id: id })
.set('Accept', 'application/json')
.end(function(err, res){});
}
Upvotes: 2