Designing and Documenting an API with Swagger: curl commands don't work

https://www.codecademy.com/paths/fscj-22-back-end-development/tracks/fscj-22-api-development-with-swagger-and-openapi/modules/wdcp-22-design-and-document-apis-with-openapi-swagger-b5ed0e68-16f7-478f-9bed-847803b52b3b/articles/designing-and-documenting-an-api-with-swagger

Hello,

whenever I try to execute the curl testing commands, like this one,

curl --header "Content-Type: application/json" -d "@new_order.json" http://localhost:3000/neworder

I always get an error in my console :

Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'Content-Type: application/json'.
At line:1 char:1
+ curl --header "Content-Type: application/json" -d "@new_order.json" h ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Does anyone know the root cause and how I can fix it ?

Thank you,
Johan (Belgium)

Never mind.

I found the solution myself . It seems that my default Visual Studio Code installation is not using the Curl you installed but a CmdLet called Invoke-WebRequest .

I needed to execute first :

Remove-item alias:curl

And the rerun the commands.

Now it works !

Johan

2 Likes

I was getting the same thing, I ended up executing the command in Git BASH and it worked

I am however having an issue getting the /delete/{id} endpoint to work. I dont get any error code, but the order just doesnt delete. I tried also doing it with the solution code provided and it still did not work.

1 Like

I am having the same issue! In the terminal it says ‘deleting order’ and ‘success’, but after refreshing the page the order does not delete. If you have solved this problem, please let me know. Thanks!

Same, the order with an id of 002 is still there after executing the command given

Tbh I found this lesson pretty hard to follow once we finished with the Swagger UI, and ended up largely just comparing the solution to what I had at each stage - even something as basic as “copy paste your api file into the project directory” isn’t ever actually mentioned (just a load of bumf about “look at the files that were generated - we’re not going to use any of them though lol”)

Neither surprised nor impressed to find that, yet again, the end result of a bunch of headscratching and forum troubleshooting leads to a final result that still doesn’t do what it says anyway

1 Like

I did some researching and after following their instructions and updating server.js file, the DELETE command ACTUALLY deletes the information:

const express = require(‘express’);
const server = express();
const fs = require(‘fs’);

// Function to read orders from the file and parse it as JSON
function readOrdersFromFile() {
const data = fs.readFileSync(‘orders.json’, ‘utf8’);
return JSON.parse(data);
}

let orderData = readOrdersFromFile();
server.set(‘port’, process.env.PORT || 3000);

server.get(‘/’,(request,response)=>{
response.send(‘Welcome to our simple online order managing web app!’);
});

server.get(‘/orders’,(request,response)=>{
response.json(orderData);
});

server.post(‘/neworder’, express.json(), (request,response)=>{
orderData.orders.push(request.body);
fs.writeFileSync(‘orders.json’, JSON.stringify(orderData));
response.send(“Success”)
console.log(“Success”);
});

server.put(‘/update/:id’, express.text({type: ‘/’}), (request,response)=>{
var items = orderData.orders

items.forEach(function(o) {
console.log(o)
if (o.id == request.params.id){
console.log(‘Modifying order!’)
o.state = request.body;
}
});

fs.writeFileSync(‘orders.json’, JSON.stringify(orderData));

response.send(“Success”);
console.log(“Success”);
});

server.delete(‘/delete/:id’, (request, response) => {
const orderId = request.params.id;
const orderIndex = orderData.orders.findIndex((o) => o.id === orderId);

  if (orderIndex !== -1) {
    // Remove the order from the orders array using splice
    orderData.orders.splice(orderIndex, 1);

    // Write the updated orderData back to the file
    fs.writeFileSync('orders.json', JSON.stringify(orderData));

    console.log('Deleting order!');
    response.send("Success");
  } else {
    console.log('Order not found!');
    response.status(404).send("Order not found!");
  }
});

server.listen(3000,()=>{
console.log(‘Express server started at port 3000’);
});

Using the original solution code, the orders.json file does delete the order, it’s just the /orders/ path does not reflect this. If you restart the server, the change takes place. Not exactly sure why this only occurs for the DELETE path and not the PUT path. Might it have to do with the middleware (or lack there of in the DELETE path)?