关于的python请求参数

注:请求头格式

1. url带请求参数,参数为数字类型

@GetMapping("/book/{id}")
    public Book getBookById(@PathVariable("id") Long id) {
        return null;
    }
headers = {'Content-type': 'application/json', 'access-token': ''}
        res_book = requests.get('http://localhost:8088/api/v1/book/1', headers=headers)
        self.assertTrue(res_book.status_code == requests.codes.ok)
        book_id1 = 1
        res_book1 = requests.get(f'http://localhost:8088/api/v1/book/{book_id1}', headers=headers)
        self.assertTrue(res_book1.status_code == requests.codes.ok)
        book_id2 = "2"
        res_book2 = requests.get(f'http://localhost:8088/api/v1/book/{int(book_id2)}', headers=headers)
        self.assertTrue(res_book2.status_code == requests.codes.ok)

 2. url带请求参数,参数为字符串类型

@GetMapping("/book/author/{name}")
    public Book getBookByAuthorName(@PathVariable("name") String name) {
        return null;
    }
headers = {'Content-type': 'application/json', 'access-token': ''}
        res_book = requests.get('http://localhost:8088/api/v1/book/author/Bruce', headers=headers)
        self.assertTrue(res_book.status_code == requests.codes.ok)
        author_name = 'Bruce'
        res_book1 = requests.get(f'http://localhost:8088/api/v1/book/author/{author_name}', headers=headers)
        self.assertTrue(res_book1.status_code == requests.codes.ok)
        # 生成4位随机字符串
        author_name2 = ''.join(random.sample(string.ascii_letters + string.digits, 4))
        res_book2 = requests.get(f'http://localhost:8088/api/v1/book/author/{author_name2}', headers=headers)
        self.assertTrue(res_book2.status_code == requests.codes.ok)

3. url不带请求参数,参数为字符串类型

@GetMapping("/book")
    public Book getBookByName(String name) {
headers = {'Content-type': 'application/json', 'access-token': ''}
        params = {'name': 'java'}
        res_book = requests.get('http://localhost:8088/api/v1/book', headers=headers, params=params)
        self.assertTrue(res_book.status_code == requests.codes.ok)

4. url不带请求参数,参数为Body类型

@PostMapping("/book")
    public Book updateBook(@RequestBody Book book) {
headers = {'Content-type': 'application/json', 'access-token': ''}
        data = {'id': 1, 'name': 'java'}
        res_book = requests.post('http://localhost:8088/api/v1/book', headers=headers, data=json.dumps(data))
        self.assertTrue(res_book.status_code == requests.codes.ok)
        # Content-type不同
        headers = {'Content-type': 'application/x-www-form-urlencoded'}
        res_book1 = requests.post('http://127.0.0.1:8088/api/v1/book', headers=headers, data=data)

5. url不带请求参数,参数为对象类型

@PostMapping("/author")
    public Author updateAuthor(Author author) {
headers = {'Content-type': 'application/json', 'access-token': ''}
        params = {'id': 1, 'firstName': 'Bruce'}
        res_book = requests.post('http://localhost:8088/api/v1/author', headers=headers, params=params)
        self.assertTrue(res_book.status_code == requests.codes.ok)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值