Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL - execute a command and save the Json result to file?

Tags:

curl

cmd

I am trying to run this command on CMD and the command "appears" to run without errors but I am not sure if it is actually making the request. The results should be returned in a Json file.

curl -H "Accept: application/json+v3" -H "x-api-key: <my_api_key>" \https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?registration=X182XCD

This is the API documentation.

When I ran this code in CMD, I got this error:

curl: (1) Protocol "\https" not supported or disabled in libcurl

Removing the \ from the \https:// seems to solve it. But I'm not sure if this command is executing successfully. Or whether it is executing correctly and just needs the Json results to be saved. I just need the Json results to be saved to a file where I can then process them.

EDIT 1: I ran the code as @gabriel-santos posted and this is what I got. No new file was created by the command in the root directory. enter image description here

like image 846
KickAss Avatar asked Oct 22 '25 19:10

KickAss


2 Answers

You can use -o file to store output to desired file. \ will only sscape line break, so, if you are sending an one line command you don't need it, otherwise you should:

curl -H "Accept: application/json+v3" \
-H "x-api-key: <my_api_key>" \
https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests\?registration=X182XCD \
-o myresponse.json
like image 77
Gabriel Santos Avatar answered Oct 27 '25 03:10

Gabriel Santos


Okay solved it. Thanks to @GabrielSantos's answer, this is the working code:

curl -H "Accept: application/json+v3" -H "x-api-key: <my_api_key>" https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests?registration=X182XCD -o myresponse.json

The \ for the registration was stopping the paramater being accepted and the Json was staying blank.

If I remove -o myresponse.json, the result is printed on the CMD, which is also useful.

Lesson: If entire code going to be single CMD command, remove EVERY \.

Hope this helps!

like image 40
KickAss Avatar answered Oct 27 '25 04:10

KickAss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!