Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何用micropython语言将图像发送到API_Image_Api_Flask_Esp8266_Micropython - Fatal编程技术网

Image 如何用micropython语言将图像发送到API

Image 如何用micropython语言将图像发送到API,image,api,flask,esp8266,micropython,Image,Api,Flask,Esp8266,Micropython,我正在尝试通过Micropython中的API发送图像。仍然没有解决办法如何做到这一点。请帮忙 import urequests import json URL = 'https://example.com/test' datas = json.dumps({"auth_key": "43435", "mac": "abcd", "name": "washid"}) filep = 'OBJ.jpg' filess = {'odimg': open(filep, 'rb')} try:

我正在尝试通过Micropython中的API发送图像。仍然没有解决办法如何做到这一点。请帮忙

import urequests
import json
URL = 'https://example.com/test'
datas = json.dumps({"auth_key": "43435", "mac": "abcd", "name": "washid"})
filep = 'OBJ.jpg'
filess = {'odimg': open(filep, 'rb')}
try:
    response = urequests.post(URL,data=datas,files=files)
    print(response.json())
except Exception as e:
    print(e)

也许此模板可以帮助您:

import ubinascii
import uos
import urequests


def make_request(data, image=None):
    boundary = ubinascii.hexlify(uos.urandom(16)).decode('ascii')

    def encode_field(field_name):
        return (
            b'--%s' % boundary,
            b'Content-Disposition: form-data; name="%s"' % field_name,
            b'', 
            b'%s'% data[field_name]
        )

    def encode_file(field_name):
        filename = 'latest.jpeg'
        return (
            b'--%s' % boundary,
            b'Content-Disposition: form-data; name="%s"; filename="%s"' % (
                field_name, filename),
            b'', 
            image
        )

    lines = []
    for name in data:
        lines.extend(encode_field(name))
    if image:
        lines.extend(encode_file('file'))
    lines.extend((b'--%s--' % boundary, b''))
    body = b'\r\n'.join(lines)

    headers = {
        'content-type': 'multipart/form-data; boundary=' + boundary,
        'content-length': str(len(body))}
    return body, headers


def upload_image(url, headers, data):
    http_response = urequests.post(
        url,
        headers=headers,
        data=data
    )
    if http_response.status_code == 204:
        print('Uploaded request')
    else:
        raise UploadError(http_response)
    http_response.close()
    return http_response

你需要为你的请求声明一个标题

谢谢你的回复jonathan,我是micropython新手,你能告诉我步骤吗,我应该调用make-request()并在上传图像中传递图像吗