问题
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
.
Then during the data file upload via
PUT
, will set the right media type. If thePUT
is Not success, the response should be415
Second method could be by adding content-encoding during
POST
. If the encoding is not supported the response should be415
.
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