玩转WEB接口之一 【HTTP调试测试神器 httpbin使用】

一、项目、作者何许人也

httpbin是大神kennethreitz为搭建测试用的http服务而写的项目,httpbin 这个服务应用能测试HTTP请求及响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试具有很大的帮助,它是用 Python + Flask 编写的,是一个开源项目。

Kenneth Reitz: 著名的python首席架构师,requests、python-guide、pipenv、legit、autoenv等一系列大名鼎鼎的python类库及工具的创作者,大神级程序员,据说其身价高达5亿美元。

二、观摩庐山真面目

官网 https://httpbin.org/

http方法组展开的接口
在这里插入图片描述
其他的分组

在这里插入图片描述
点击 HTML form 是一个form表单提交页面,可以用来测试
在这里插入图片描述

三、怎么玩

以get请求为例:

展开 HTTP Methods 方法下面的get请求,选择Try it out,
在这里插入图片描述

出现 Execute 点击他, Response body 返回json数据。
在这里插入图片描述
依次类推,post请求返回数据如下:
在这里插入图片描述
包含form表单、json数据、header、url、origin 等信息。

四、后端怎么调用

1. RestTemplate测试代码

这里选取了四个典型场景: get请求、formdata表单提交、json字符串post提交、带文件 multipart/form-data 提交
在这里插入图片描述

import java.util.Arrays;

import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RestTemplateSimpleTest
{
   
    private static RestTemplate restTemplate;
    
    static
    {
   
        int type = RandomUtils.nextInt(1, 5);
        log.info("--------- type:{}", type);
        switch (type)
        {
   
            case 1:
                // 使用默认客户端构造RestTemplate,写法1
                /*** Java HttpURLConnection (默认RestTemplate采用,不支持HTTP2) ***/
                restTemplate = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值