Simple interface calls, perfect completion of shortening the site

Keywords: node.js network Go

Short web addresses are sometimes a very new requirement. There are many such websites on the Internet, but sometimes they want to call interfaces to shorten the web addresses. Many of these websites are not provided. Commonly used interfaces on the Internet, such as Sina's short web interface, shallot computing Short web interface In fact, they are all very useful. Now take the short web interface of shallot computing as an example, use nodejs to show how to use it.

var http = require('http'); 
var qs = require('querystring'); 

//Configure the appKey and openId you applied for
app_key = "***";
open_id = "***";
 
function request_content(request_url,port,params,method){
    
    var path=request_url;
    if(!!params){
        var content = qs.stringify(params);  
        path=request_url+'?' + content;
    }
    
    var options = { 
        port: port,
        path: path,
        method: method
    }; 
    
    if(method.toLowerCase()=='post'){
        options['headers']="Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8";
    }

    var req = http.request(options, function (res) { 
        res.setEncoding('utf8'); 
        res.on('data', function (chunk) { 
            console.log(chunk); 
        }); 
    });  
    
    req.on('error', function (e) { 
        console.log('problem with request: ' + e.message); 
    }); 
    
    req.end();
}

function main(){

    var domain="http://api.xiaocongjisuan.com/";
    var port=8080;//http corresponds to 80 ports, https corresponds to 443 ports, please users correct themselves
    var servlet="life/shorturl/get";
    var method="get";
    var request_url=domain+servlet;

    var params = {}; 
    params['appKey']=app_key;
    params['openId']=open_id;
    
    //Variable part
    params["url"]="https://www.baidu.com/";
    
    request_content(request_url,port,params,method);
}

main();

Of course, the implementation of go language is quite simple, as follows:

package main

import (
    "io/ioutil"
    "net/http"
    "net/url"
    "fmt"
    "strings"
)
 
//Configure the appKey and openId you applied for
const APP_KEY ="yours";
const OPEN_ID ="yours";

func requestContent(requestUrl string,params url.Values,method string)(rs[]byte ,err error){
    
    if strings.ToUpper(method)=="GET"{
        return get(requestUrl,params)
    }
    return post(requestUrl,params)
}

// get network request
func get(requestUrl string,params url.Values)(rs[]byte ,err error){
    var Url *url.URL
    Url,err=url.Parse(requestUrl)
    if err!=nil{
        fmt.Printf("analysis url error:\r\n%v",err)
        return nil,err
    }
    //If there are Chinese parameters in the parameters, this method will do URLEncode.
    Url.RawQuery=params.Encode()
    resp,err:=http.Get(Url.String())
    if err!=nil{
        fmt.Println("err:",err)
        return nil,err
    }
    defer resp.Body.Close()
    return ioutil.ReadAll(resp.Body)
}
 
// post network request, params is url.Values type
func post(requestUrl string, params url.Values)(rs[]byte,err error){
    resp,err:=http.PostForm(requestUrl, params)
    if err!=nil{
        return nil ,err
    }
    defer resp.Body.Close()
    return ioutil.ReadAll(resp.Body)
}

func main(){

    domain :="http://api.xiaocongjisuan.com/"

    servlet :="life/shorturl/get"
    method :="get"
    requestUrl:=domain+servlet
    
    //Initialization parameters
    params:=url.Values{}
    
    params.Set("appKey",APP_KEY)
    params.Set("openId",OPEN_ID)
    
    //Variable part
    params.Set("url","https://www.baidu.com/")
 
 
    //Send request
    data,err:=requestContent(requestUrl,params,method)
    fmt.Println(string(data))
    if err!=nil{
        fmt.Printf("analysis url error:\r\n%v",err)
    }
}    

Call api implementation, not too much technical complexity, thank you for reading.

Posted by fodder on Thu, 10 Oct 2019 07:12:15 -0700