Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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
如何使用python通过POST请求向WordPress REST API发送带有元数据(标题、说明)的媒体_Python_Wordpress_Post_Python Requests_Request - Fatal编程技术网

如何使用python通过POST请求向WordPress REST API发送带有元数据(标题、说明)的媒体

如何使用python通过POST请求向WordPress REST API发送带有元数据(标题、说明)的媒体,python,wordpress,post,python-requests,request,Python,Wordpress,Post,Python Requests,Request,我正在尝试使用python请求向Wordpress REST API发出POST请求。帖子数据包含图像和元数据(如标题和描述)。图片上传成功,没有任何问题,但标题、说明、内容等都没有问题,。。。数据包括在内。我错过了什么?这是我的密码 header = {'Authorization': 'Basic ' + token.decode('utf-8'), "User-Agent": "Mozilla/5.0 (Windows NT

我正在尝试使用python请求向Wordpress REST API发出POST请求。帖子数据包含图像和元数据(如标题和描述)。图片上传成功,没有任何问题,但标题、说明、内容等都没有问题,。。。数据包括在内。我错过了什么?这是我的密码

    header = {'Authorization': 'Basic ' + token.decode('utf-8'),
              "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
    }

    media = {
         'file': open("img.jpg","rb"),
         'title': "title",
         'caption': "caption",
         'description': "description",
         'alt_text': "alt_text"
    }

    image =  requests.post(url + '/media' , headers= header, files= media,timeout=15)

我想你可能需要提出两个请求,第一个是上传图片,第二个是设置标题和描述。以下代码应该适合您(主要取自[1]):


[1]

您的标题中到底有什么?我确实添加了标题。。。由于此文档…需要“附件标题”。。。我怎样才能设定。。。还有tnx编辑我的帖子..我的英语不好@buddematMy错了。见下面我的答案。
headers = {'Authorization': 'Basic ' + token.decode('utf-8')} 

media = {'file': open('img.jpg','rb')}

# first request uploading the image
image = requests.post(url + '/media', headers=headers, files=media) 

# get post-id out of the response to the first request
postid =json.loads(image.content.decode('utf-8'))['id'] 

# set the meta data
post = {'title': 'title',
        'caption': 'caption',
        'description': 'description',
        'alt_text': 'alt_text'
       }

# second request to send the meta data
req = requests.post(url + '/media/'+str(postid), headers=headers, json=post)