I'm trying to upload a file to Drupal 8 through REST API. I enabled the file resource in REST resources.
This is what my code looks like in JS:
const uri = `${constants.URL}/entity/file?_format=json`;
const authorization = `Bearer ${session.accessToken}`;
var reqData = {
"_links":{"type":{"href":`${constants.URL}/rest/type/file/image`}},
"uri":{"value":"public://attachements/test.png"},
"filename":{"value":"test.png"},
"filemime":{"value":"image/png"},
"type":{"target_id":"image"},
"data":[{"value":"[base64]"}],
"uid":[{"target_id":"1"}]
};
The result for the request is: Method Not Allowed. Or all sorts of strange things, depending on different things I try.
Inside User/Permissions I couldn't find anything to set to give permission.
Updated code -> 'The type link relation must be specified.'
const uri = `${constants.URL}/entity/file?_format=hal_json`;
const authorization = `Bearer ${session.accessToken}`;
var reqData = {
"_links":{"type":{"href":`${constants.URL}/rest/type/file/image`}},
"uri":[{"value":"public://test.png"},{"url":"/sites/default/files/test.png"}],
"filename":{"value":"test.png"},
"filemime":{"value":"image/png"},
"type":{"target_id":"image"},
"data":[{"value":`${base64Data}`}],
"uid":[{"target_id":"1"}]
};
const response = await axios({
method: 'POST',
url: uri,
data: {reqData},
headers: {
"Content-Type": "application/hal+json",
"Accept": "application/hal+json",
"Authorization": authorization
}
})