how to reproduce 415 Unsupported Media type

我的未来我决定 提交于 2021-01-29 10:28:11

问题


How to reproduce 415 Unsupported media type?

I am trying with this with the upload to Google Drive. Setup resumable upload so that I can add any media after the initial POST. Thinking like during the first POST, the media type is not set completely. Google Drive will use some default option like application/octet-stream.

  1. Then during the data file upload via PUT, will set the right media type. If the PUT is Not success, the response should be 415

  2. Second method could be by adding content-encoding during POST. If the encoding is not supported the response should be 415.

Here is Python code I rewrote from Web. How to change this to get 415? Are there other methods to get this error? I am not familiar with Javascript, web resources are cryptic for me now.

import json
import os
import requests


access_token ="ya29.XXX.....XXXqI"
filename = './test-atom.xml'

filesize = os.path.getsize(filename)

# 1. Retrieve session for resumable upload.

headers = {"Authorization": "Bearer "+access_token, "Content-Type": "application/json"}

params = {
    "name": "drive-small2.xml",
    #"mimeType": "application/atom+xml" ## works without this line
}

r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
    headers=headers,
    data=json.dumps(params)
)

location = r.headers['Location']

# 2. Upload the file.


#headers = {"Content-Type": "application/atom+xml", "Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)} ## works for this also
headers = { "Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)} ## works for this also
r = requests.put(
    location,
    headers=headers,
    data=open(filename, 'rb')
)
print(r.text)

***********************

C:\Users\Administrator\Desktop\driveUpload>py resume-upload10.py
{
 "kind": "drive#file",
 "id": "1nQW6_-1F4fBEcKoln7vSM0qOTpWhSpZ2",
 "name": "drive-small2.xml",
 "mimeType": "text/xml"
}

来源:https://stackoverflow.com/questions/65518211/how-to-reproduce-415-unsupported-media-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!