shell脚本批量调用接口

20次阅读

共计 1305 个字符,预计需要花费 4 分钟才能阅读完成。

正文

  要求在页面查询到 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

执行脚本

sh batch_curl.sh gege 21

执行结果

该接口是用 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()}



本公众号 免费 提供 csdn 下载服务,海量 IT 学习资源,如果你准备入 IT 坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于 java、go、python、springcloud、elk、嵌入式、大数据、面试资料、前端 等资源。同时我们组建了一个技术交流群,里面有很多大佬,会不定时分享技术文章,如果你想来一起学习提高,可以公众号后台回复【2】,免费邀请加技术交流群互相学习提高,会不定期分享编程 IT 相关资源。


扫码关注,精彩内容第一时间推给你

正文完
 0