Back4app uploading object issue

403 Views Asked by At

I'm trying to create an object through back4app for class sound

the issue which am having that am unable to upload the file.

code used:

import requests

headers = {
    "X-Parse-Application-Id": "hidden",
    "X-Parse-REST-API-Key": "hidden",
}


data = {
    "audio": {
        "__type": open("a.mp3", 'rb'),
        "name": "a.mp3"

    },
    "displayText": "test"
}


def main(url):
    with requests.Session() as req:
        req.headers.update(headers)
        r = req.post(url, data=data)
        print(r.text)


main("https://parseapi.back4app.com/classes/sounds")

Output:

{"code":111,"error":"schema mismatch for sounds.audio; expected File but got Array"}

enter image description here

1

There are 1 best solutions below

3
On

You first need to upload the file:

import json,httplib
connection = httplib.HTTPSConnection('parseapi.back4app.com', 443)
connection.connect()
connection.request('POST', '/files/a.mp3', open('a.mp3', 'rb').read(), {
       "X-Parse-Application-Id": "${APPLICATION_ID}",
       "X-Parse-REST-API-Key": "${REST_API_KEY}",
       "Content-Type": "audio/mpeg"
     })
result = json.loads(connection.getresponse().read())

Then create the object:

connection.request('POST', '/classes/sounds', json.dumps({
       "displayText": "test",
       "audio": {
         "name": result["name"],
         "url:": result["url"],
         "__type": "File"
       }
     }), {
       "X-Parse-Application-Id": "${APPLICATION_ID}",
       "X-Parse-REST-API-Key": "${REST_API_KEY}",
       "Content-Type": "application/json"
     })
connection.getresponse().read()