关于的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)
发布了129 篇原创文章 · 获赞 14 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/zsx18273117003/article/details/105627712
今日推荐