shell脚本批量调用接口

  要求在页面查询到5000条数据,为了方便插入,用shell脚本写curl命令调用自己写的代码接口;

脚本如下:

#!/bin/bash
a=0
while [ $a -le 10 ]; do
 
  # length of ts is 13 required,Through the following way like this
  ts=`date +%s%N`      
  ts=${ts:0:13}
  
  json='{"name" : "'$1$a'", "age" : '$2', "ts" : '$ts'}'
  
  a=$((a+1))
  
  curl -k -H 'Content-Type:application/json;charset=utf-8' http://192.168.2.5:8080 -X POST -d "'$json'"

done
批量curl脚本

执行脚本

sh batch_curl.sh gege 21

执行结果

10次curl执行结果

该接口是用go语言提供的demo接口:如下:

  • 目录结构:


    目录结构
  • app.conf
copyrequestbody = true
  • controller.go
package controller

import (
    "github.com/astaxie/beego"
    "fmt"
)

type SayHelloController struct {
    beego.Controller
}

func (this *SayHelloController) SayHello(){

    fmt.Println("RequestBody is ", string(this.Ctx.Input.RequestBody))

    this.Ctx.Output.Header("Content-type", "application/json;charset=utf-8")
    this.Ctx.Output.SetStatus(200)
    this.Ctx.Output.Body(this.Ctx.Input.RequestBody)
}
  • router.go
package router

import (
    "github.com/astaxie/beego"
    "sayHello/controller"
)

var hello = controller.SayHelloController{}

func init() {

    beego.Router("/", &hello, "POST:SayHello")
}
  • main.go
package main

import (
    "github.com/astaxie/beego"
    _ "sayHello/router"
    "fmt"
)

func main() {
    fmt.Println(beego.BConfig.CopyRequestBody)
    beego.Run()
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,168评论 19 139
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,941评论 0 5
  • Word: advanced, Sentence: I am afraid that it's beyond my...
    享悦moonlight阅读 388评论 0 0
  • 乍一看到这个话题,就觉得自己不会是移情别恋的那个人,就目前看来我还是很有幸福感的。从很多角度看移情别恋付出的代价还...
    ab7d12e1cf84阅读 749评论 0 0
  • 夜已经很深了,小姨夫刚刚落地到西宁,可是现在的你已是世间的魂魄,一抔灰... 小姨心里疼了一天了,虽然已经准备好你...
    大白菜小豆腐阅读 353评论 2 3