Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

120
Views
GET api data using curl command

I am trying to get some data from public api's using curl:

curl --request GET 'https://api.openweathermap.org/data/2.5/weather?lat=37.3565982&lon=-121.9689848&units=imperial' \
--header 'X-Api-Key: x-api-‘key \
--data 'output_format=csv' \
> save_file.csv

and if I am running the command in terminal is returning the output and everything is working just fine but if I am trying to add an extra parameter to download the result in a .csv format, is generating the file but the format inside is wrong, I mean is not added as a table format but more like a array of objects [{}, {}, {}] and so on.

It is possible to run a curl command and get the data in a csv format ?

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

--data is how you add body data to a POST request. You are making a GET request.

The api documentation for the webservice you are using says:

mode optional Response format. Possible values are xml and html. If you don't use the mode parameter format is JSON by default. Learn more

So to get something other than JSON back from the server, you need to put mode=something in the query string and only XML and HTML are options.

It is not output_format.

It does not go in the body.

CSV is not an option.


If you want to put that data into CSV format, then you'll need to transform it locally. The data format described in the documentation does not look like it lends itself to the 2D data structures described by CSV so you'll have to make decisions about how to transform it.

You've tagged this javascript so you could pipe the output of curl into a program you run with Node.js which parses the JSON, restructures it however you like, and then uses a module like papaparse to convert it to JSON.

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs