shorturl
- Print
- PDF
shorturl
- Print
- PDF
Article summary
Did you find this summary helpful?
Thank you for your feedback
Overview
nShortURL API is a REST API that converts an input URL into a short URL in the me2.do format.
Request
- POST FORM
curl -i -X POST \
-H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
-H "X-NCP-APIGW-API-KEY-ID: {Client ID issued when registering an application}" \
-H "X-NCP-APIGW-API-KEY: {Client Secret issued when registering an application}" \
-d "url={Source URL to shorten}" \
"https://naveropenapi.apigw.ntruss.com/util/v1/shorturl"
- POST JSON
curl -i -X POST \
-H "X-NCP-APIGW-API-KEY-ID:{Client ID value issued when registering an application}" \
-H "X-NCP-APIGW-API-KEY:{Client Secret issued when registering an application}" \
-H "Content-Type:application/json" \
-d '{"url": "{Source URL to shorten}"}' \
'https://naveropenapi.apigw.ntruss.com/util/v1/shorturl'
- GET
curl -i -X GET \
-H "X-NCP-APIGW-API-KEY-ID:{Client ID value issued when registering an application}" \
-H "X-NCP-APIGW-API-KEY:{Client Secret issued when registering an application}" \
'https://naveropenapi.apigw.ntruss.com/util/v1/shorturl?url={Source URL to shorten}'
Request Parameters
You can pass the following parameter to make a GET request.
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
url | string | Y | - | Original URL to shorten |
Request Header
Header | Description |
---|---|
X-NCP-APIGW-API-KEY-ID | Client ID issued when registering an appX-NCP-APIGW-API-KEY-ID:{Client ID} |
X-NCP-APIGW-API-KEY | Client Secret issued when registering an appX-NCP-APIGW-API-KEY:{Client Secret} |
Request Body
You can pass the following parameter in FORM or JSON to make a POST request.
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
url | string | Y | - | Original URL to shorten |
Response
Response Body
Field | Type | Description |
---|---|---|
hash | string | Short URL Hash Information |
url | string | Shortened URL |
orgUrl | string | Original URL |
Examples
Request Example
curl "https://naveropenapi.apigw.ntruss.com/util/v1/shorturl" \
-d "url=http://d2.naver.com/helloworld/4874130" \
-H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
-H "X-NCP-APIGW-API-KEY-ID: {Client ID issued when registering an application}" \
-H "X-NCP-APIGW-API-KEY: {Client Secret issued when registering an application}" -v
Response Example
{
"message":"ok",
"result": {
"hash":"GyvykVAu",
"url":"https://me2.do/GyvykVAu",
"orgUrl":"http://d2.naver.com/helloworld/4874130"
}
,"code":"200"
}
API examples
// Example of using the nShortURL API
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class APIExamURL {
public static void main(String[] args) {
String clientId = "YOUR_CLIENT_ID";//Application client ID";
String clientSecret = "YOUR_CLIENT_SECRET";//Application client secret";
try {
String text = "https://developers.naver.com/notice";
String apiURL = "https://naveropenapi.apigw.ntruss.com/util/v1/shorturl";
URL url = new URL(apiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("X-NCP-APIGW-API-KEY-ID", clientId);
con.setRequestProperty("X-NCP-APIGW-API-KEY", clientSecret);
// post request
JSONObject json = new JSONObject();
json.put("url", text);
String postParams = json.toString();
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader br;
if(responseCode==200) { // Normal
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);
}
}
}
// Example of using the NAVER ShortURL Open API
<?php
$client_id = "YOUR_CLIENT_ID";
$client_secret = "YOUR_CLIENT_SECRET";
$encText = urlencode("https://developers.naver.com/docs/utils/shortenurl");
$postvars = "url=".$encText;
$url = "https://naveropenapi.apigw.ntruss.com/util/v1/shorturl";
$is_post = true;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postvars);
$headers = array();
$headers[] = "X-NCP-APIGW-API-KEY-ID: ".$client_id;
$headers[] = "X-NCP-APIGW-API-KEY: ".$client_secret;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$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;
} else {
echo "Error message:".$response;
}
?>
// Example of using the nShortURL API
var express = require('express');
var app = express();
var client_id = 'YOUR_CLIENT_ID';
var client_secret = 'YOUR_CLIENT_SECRET';
var query = encodeURI('https://developers.naver.com/docs/utils/shortenurl');
app.post('/url', function(req, res) {
var api_url = 'https://naveropenapi.apigw.ntruss.com/util/v1/shorturl';
var request = require('request');
var options = {
url: api_url,
form: { url: query },
headers: { 'X-NCP-APIGW-API-KEY-ID': client_id, 'X-NCP-APIGW-API-KEY': client_secret },
};
request.get(options, function(error, response, body) {
if (!error && response.statusCode == 200) {
res.writeHead(200, { 'Content-Type': 'text/json;charset=utf-8' });
res.end(body);
} else {
res.status(response.statusCode).end();
console.log('error = ' + response.statusCode);
}
});
});
app.listen(3000, function() {
console.log('http://127.0.0.1:3000/url app listening on port 3000!');
});
# Example of using the NAVER ShortURL Open API
import os
import sys
import urllib.request
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
encText = urllib.parse.quote("https://developers.naver.com/docs/utils/shortenurl")
data = "url=" + encText
url = "https://naveropenapi.apigw.ntruss.com/util/v1/shorturl"
request = urllib.request.Request(url)
request.add_header("X-NCP-APIGW-API-KEY-ID",client_id)
request.add_header("X-NCP-APIGW-API-KEY",client_secret)
response = urllib.request.urlopen(request, data=data.encode("utf-8"))
rescode = response.getcode()
if(rescode==200):
response_body = response.read()
print(response_body.decode('utf-8'))
else:
print("Error Code:" + rescode)
using System;
using System.Net;
using System.Text;
using System.IO;
namespace NaverAPI_Guide
{
public class APIExamURL
{
static void Main(string[] args)
{
string url = "https://naveropenapi.apigw.ntruss.com/util/v1/shorturl";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("X-NCP-APIGW-API-KEY-ID", "YOUR_CLIENT_ID"); // Client ID
request.Headers.Add("X-NCP-APIGW-API-KEY", "YOUR_CLIENT_SECRET"); // Client Secret
request.Method = "POST";
string query = "https://developers.naver.com/docs/utils/shortenurl"; // URL to shorten
byte[] byteDataParams = Encoding.UTF8.GetBytes("url=" + query);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteDataParams.Length;
Stream st = request.GetRequestStream();
st.Write(byteDataParams, 0, byteDataParams.Length);
st.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string text = reader.ReadToEnd();
stream.Close();
response.Close();
reader.Close();
Console.WriteLine(text);
}
}
}
Error Codes
HTTP Code | Error code | Error message | Measure |
---|---|---|---|
403 | 1403 | Invalid URL(Request URL error) | |
403 | 2403 | Forbidden URL(API usage permission error) | |
403 | 3403 | Unavailable URL(URL that does not exist or is not secure) | |
500 | 1500 | Internal server error(Internal server error) | Please contact our customer support. |
Was this article helpful?