MENU
      Request image file

        Request image file


        Article summary

        Available in Classic and VPC

        This document describes examples of using the Request CAPTCHA image file API.

        Request CAPTCHA image file

        This section describes API examples that request a CAPTCHA image file using an issued CAPTCHA key.

        Java

        The following is a Java-based sample code for the Request CAPTCHA image file API.

        import java.io.BufferedReader;
        import java.io.InputStreamReader;
        import java.net.HttpURLConnection;
        import java.net.URL;
        import java.net.URLEncoder;
        
        public class APIExamCaptchaNkey {
        
            public static void main(String[] args) {
                String clientId = "YOUR_CLIENT_ID";//Application's client ID value";
                String clientSecret = "YOUR_CLIENT_SECRET";//Application's client secret value";
                try {
                    String code = "0"; // Set to 0 for image file request and 1 for CAPTCHA image comparison
                    String apiURL = "https://naveropenapi.apigw.ntruss.com/captcha/v1/nkey?code=" + code;
                    URL url = new URL(apiURL);
                    HttpURLConnection con = (HttpURLConnection)url.openConnection();
                    con.setRequestMethod("GET");
                    con.setRequestProperty("X-NCP-APIGW-API-KEY-ID", clientId);
                    con.setRequestProperty("X-NCP-APIGW-API-KEY", clientSecret);
                    int responseCode = con.getResponseCode();
                    BufferedReader br;
                    if(responseCode==200) { // Successful call
                        br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    } else {  // Error occurred
                        br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
                    }
                    String inputLine;
                    StringBuffer response = new StringBuffer();
                    while ((inputLine = br.readLine()) != null) {
                        response.append(inputLine);
                    }
                    br.close();
                    System.out.println(response.toString());
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
        Java

        Python

        The following is a Python-based sample code for the Request image file API.

        import os
        import sys
        import urllib.request
        client_id = "YOUR_CLIENT_ID"
        key = "YOUR_CAPTCHA_KEY" # CAPTCHA key value
        url = "https://naveropenapi.apigw.ntruss.com/captcha-bin/v1/ncaptcha?key=" + key + "&X-NCP-APIGW-API-KEY-ID=" + client_id;
        request = urllib.request.Request(url)
        response = urllib.request.urlopen(request)
        rescode = response.getcode()
        if(rescode==200):
            print("Save CAPTCHA image")
            response_body = response.read()
            with open('captcha.jpg', 'wb') as f:
                f.write(response_body)
        else:
            print("Error Code:" + rescode)
        Python

        PHP

        The following is a PHP-based sample code for the Request image file API.

        <?php
          $client_id = "YOUR_CLIENT_ID";
          $key = "CAPTCHA_KEY";
          $url = "https://naveropenapi.apigw.ntruss.com/captcha-bin/v1/ncaptcha?key=".$key."&X-NCP-APIGW-API-KEY-ID=".$client_id;
          $is_post = false;
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, $is_post);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $response = curl_exec ($ch);
          $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          echo "status_code:".$status_code."<br />";
          curl_close ($ch);
          if($status_code == 200) {
            //echo $response;
            $fp = fopen("captcha.jpg", "w+");
            fwrite($fp, $response);
            fclose($fp);
            echo "<img src='captcha.jpg'>";
          } else {
            echo "Error content:".$response;
          }
        ?>
        PHP

        JavaScript

        The following is a JavaScript-based sample code for the Request image file API.

        var express = require('express');
        var app = express();
        var client_id = 'YOUR_CLIENT_ID';
        var fs = require('fs');
        app.get('/captcha/image', function (req, res) {
           var api_url = 'https://naveropenapi.apigw.ntruss.com/captcha-bin/v1/ncaptcha?key=' + req.query.key + "&X-NCP-APIGW-API-KEY-ID=" + client_id;
           var request = require('request');
           var options = {
               url: api_url
            };
            var writeStream = fs.createWriteStream('./captcha.jpg');
            var _req = request.get(options).on('response', function(response) {
               console.log(response.statusCode) // 200
               console.log(response.headers['content-type'])
            });
          _req.pipe(writeStream); // Output to file
          _req.pipe(res); // Output to browser
         });
         app.listen(3000, function () {
           console.log('http://127.0.0.1:3000/captcha/image?key=캡차키 app listening on port 3000!');
         });
        JavaScript

        C#

        The following is a C#-based sample code for the Request image file API.

        using System;
        using System.Net;
        using System.Text;
        using System.IO;
        
        namespace NaverAPI_Guide
        {
            public class APIExamCaptchaImage
            {
                static void Main(string[] args)
                {
                    string key = "KEY-INPUT"; // Key value received from the https://naveropenapi.apigw.ntruss.com/captcha/v1/nkey call
                    string url = "https://naveropenapi.apigw.ntruss.com/captcha/v1/ncaptcha?key=" + key + "&X-NCP-APIGW-API-KEY-ID=YOUR-CLIENT-ID";
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    string status = response.StatusCode.ToString();
                    Console.WriteLine("status="+ status);
                    using (Stream output = File.OpenWrite("c:/captcha.jpg"))
                    using (Stream input = response.GetResponseStream())
                    {
                        input.CopyTo(output);
                    }
                    Console.WriteLine("c:/captcha.jpg was created");
                }
            }
        }
        Plain text

        Was this article helpful?

        Changing your password will log you out immediately. Use the new password to log back in.
        First name must have atleast 2 characters. Numbers and special characters are not allowed.
        Last name must have atleast 1 characters. Numbers and special characters are not allowed.
        Enter a valid email
        Enter a valid password
        Your profile has been successfully updated.