api:python requests模块发送post请求

1 python简单发送get请求

 

2 python简单发送post请求(json)

如下,发送图片网络地址请求AI人工智能服务器提供相似图片集返回的请求

# coding=utf-8
import json
import requests

def send_api(id,img_url):

    headers = {}
    headers["Content-Type"] = "application/json"
    headers["Cache-Control"] = "no-cache"
    print(headers)

    chaomy={}
    url = "http://api.xxxxxx.com/carpet"
    chaomy["id"] = id
    chaomy["img_url"] = img_url
    payload = chaomy
    print(payload)

    try:
        result = requests.post(url, json=payload,headers=headers)
        print("result.status_code: ", result.status_code)
        print("result.content: ",json.loads(result.content))
        return json.loads(result.content)
    except Exception as e:
        print("requests failed: ",e)
        result = None

    return result

if __name__ == '__main__':

    id = "4c2691d3385b945d99a93f80ed3a79c9"
    img_url = "http://xxxx:9090/carpet_images/4c/26/91/4c2691d3385b945d99a93f80ed3a79c9.jpg"
    data = send_api(id,img_url)
    print(data)

 

posted @ 2018-05-28 16:33  Adamanter  阅读(348)  评论(0编辑  收藏  举报