使用curl post json到webApi

1.windows安装curl,配置环境变量

我这里下载的是curl-7.33.0-win64-ssl-sspi.zip,解压。

给Windows增加curl命令的环境变量,增加CURL_HOME环境变量,给PATH环境变量加上%CURL_HOME%;  

CURL_HOME     D:\curl

PATH                     %CURL_HOME%

这样就可以在命令窗口的任意目录下使用curl命令了。


2.springmvc rest接口


这里仅展示一个方法
// 供ftp上传文件使用
	@RequestMapping(value = "/ftpUpdate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public HashMap<String, String> ftpUpdate(@RequestBody Map<String,Object> reqMap) {
		HashMap<String, String> resMap = new HashMap<String, String>();
		resMap.put("msg", "success");
		//参数处理
		String	ename = (String) reqMap.get("appName");
		boolean	apkUpdate = (Boolean)reqMap.get("apkUpdate");
		boolean	ipaUpdate = (Boolean)reqMap.get("ipaUpdate");
		String	versionId = (String) reqMap.get("versionId");
		if (ename == null) {
			resMap.put("msg", "参数appName缺失异常");
			return resMap;
		}
		if (reqMap.get("apkUpdate") == null) {
			resMap.put("msg", "参数isApkUpdate缺失异常");
			return resMap;
		}
		if (reqMap.get("ipaUpdate") == null) {
			resMap.put("msg", "参数isIosUpdate缺失异常");
			return resMap;
		}
		if (versionId == null) {
			resMap.put("msg", "参数versionId缺失异常");
			return resMap;
		}
		//更新信息维护
		App oldApp=new App();
		try {
			oldApp = appService.getAppByEname(ename);

		} catch (Exception e) {
			resMap.put("msg", "没有这个app");
			return resMap;
			// TODO: handle exception
		}

			String rootPath = UploadFileController.class.getResource("/").getFile();
			rootPath = rootPath.substring(1, rootPath.length() - 8);
			String apkPath = rootPath + "file" + "/apk/" + ename + ".apk";
			String ipaPath = rootPath + "file" + "/ipa/" + ename + ".ipa";
			if (!versionId.equals("false")) {
				oldApp.setVersionId(versionId);
			}
			if (apkUpdate) {
				File file=new File(apkPath);
				if (file.exists()) {
					String apkSize  = FileOpearte.getFileSize(file);
					oldApp.setFileSize(apkSize);
					appService.updateApp(oldApp);
					FileOpearte.backupFile(oldApp.getId(),ename, oldApp.getVersionId(), oldApp.getUpdateDate(),"apk");
				}
			}
			if (ipaUpdate) {
				File file=new File(ipaPath);
				if (file.exists()) {
					oldApp = appService.getAppByEname(ename);
					String ipaSize  = FileOpearte.getFileSize(file);
					oldApp.setIpaFileSize(ipaSize);
					appService.updateApp(oldApp);
					FileOpearte.backupFile(oldApp.getId(),ename,oldApp.getVersionId(), oldApp.getUpdateDate(),"ipa");
				}
			}
			return resMap;
	}

3.使用curl命令post json到后台

cmd到命令窗口,输入命令:
curl -l -H "Content-Type:application/json" -H "Accept:application/json" -X POST -d {\"appName\":\"test\",\"apkUpdate\":true,\"ipaUpdate\":true,\"versionId\":\"false\"} http://xx.xx.xx.xx:8899/AppStore/file/ftpUpdate

成功后的返回自定义信息:




注意事项:
        -H                     请求头
        -X POST          发送post请求
        json格式          不用单引号''包括,
                                  双引号用反斜杠\转义
                                  参数和后台参数保持一致,我这里用map接收也就不存在这种情况
如果不注意上述注意事项会报错:
                                    1.The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
                                    2.The request sent by the client was syntactically incorrect
                                    3.如果是测试本地接口 ,使用localhost会报错:could  not resolve host: localhost      (解决:使用ip地址替代)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值