3

I am using the curl to get git repositories information. The command used is:

curl https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc

However, what I got is:

{
  "message": "Validation Failed",
  "errors": [
    {
      "resource": "Search",
      "field": "q",
      "code": "missing"
    }
  ],
  "documentation_url": "https://developer.github.com/v3/search"
}

If I type directly the URL in the browser I obtain the correct JSON output, but when I use curl it fails. Any solution?

Armfoot
  • 4,663
  • 5
  • 45
  • 60
Aliu
  • 99
  • 9

1 Answers1

0

In curl's documentation they actually only show in one of their examples:

Get the main page from an IPv6 web server: curl "http://[2001:1890:1112:1::20]/"

"" are required to surround your URL (due to its complexity). The command should be like this then:

curl "https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc"

If you want it to be downloaded to a file, then you can use the -O option:

curl -O "https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc"

If you're just using windows command line then you can run the winssl's curl from this zip (you need the ssl version for https).

However, I noticed your javascript tag in your question, so if you're planning to use curl on the client side, the curl.js plugin may be useful to you.

On the server side, if you are using PHP you can use this code:

<?
    $url="https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc";
    
    //  Initiate curl
    $ch = curl_init();
    // Disable SSL verification
    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Set the url
    curl_setopt($ch, CURLOPT_URL,$url);
    // Execute
    $result=curl_exec($ch);
    // Closing
    curl_close($ch);
    
    // Will dump a beauty json :3
    var_dump(json_decode($result, true));
?>

This code was placed by "none" in here, but in your case, you may need SSL verification enabled (so I commented it).

curl_setopt allows you to set these required options (and the URL itself) when using curl for getting a json file.

Community
  • 1
  • 1
Armfoot
  • 4,663
  • 5
  • 45
  • 60