• 用Python发生RestFul API POST和GET请求


    使用Python调Restful API

    本文给出用GET方法从Restful API获取信息和用POST方法向Restful API发生消息。主要使用的包是urllib和json,其中urllib用来发送http请求,json包用来解析和构造json数据。具体例子如下:

    通过GET方法获取信息

    import json
    from urllib import request
    
    query_url_addr='' #the Restful api url
    query_headers={'cookie':'the cooke'} #the request headers
    req = request.Request(query_url_addr, headers=query_headers)
    resp = request.urlopen(req)
    result = resp.read().decode()
    result_json = json.loads(result)#the json object of response data
    

    用POST方法向Restful API发生消息

    import json
    import time
    from urllib import request
    from urllib import error
    try:
        create_url=''#the create request url
        create_headers={'cookie':'the cooke'} #the request headers
        body_data_str='{"body":"bodytext"}'
        body_data = bytes(body_data_str, 'utf8')
        req = request.Request(create_url, headers=create_headers, data=body_data, method='POST')
        resp = request.urlopen(req)
        result = resp.read().decode()
        result_json = json.loads(result)
        return result_json
    except error.HTTPError as err:
         error_body = err.file.read().decode()
         return  json.loads(result)
    
  • 相关阅读:
    laravel-13-笔记-1
    laravel-14-笔记-2
    supervisor监听器-linux安装配置
    laravel-12-artisan命令创建view文件
    linux修改主机名
    laravel-11-laravel 模型Eloquent ORM
    laravel-composer安装laravel
    laravel-10-laravel collection集合
    laravel-8-laravel数据填充
    laravel-9-laravel数据库查询 查询组件
  • 原文地址:https://www.cnblogs.com/dereklovecc/p/12634202.html
Copyright © 2020-2023  润新知