API patch file-update call

hi!

i need to add a field to the content of a file (eg filename.pdf.txt). i keep getting a 400 status response, Bad Request.

as it was not immediately clear how to pass a template when uploading a file (hint: make a new FormData.append('template', '<template-name>'), i report here what i’m doing.

my workflow right now is:

  • make POST request and add file and file template
  • if operation goes well, make PATCH request and pass object to above created file

the PATCH url is as follow

https://<website>/api/pages/<main-page>+<subpage>/files/<filename>

i tried adding also .txt after the <filename>, still error.

the filename is retrieved from the response of the POST call, under response.data.filename.

the PATCH object i pass to the body of the xhr call is

{ key: 'value'}

i tried to pass the above object also as JSON.stringify, no better luck.

i also tried to directly add another field in the FormData with the content i want to write, but that POST action takes only the template argument as optional argument.

how would you send a correct file PATCH call?

thanks, AF

I’m not sure I’m following you.

It works as documented in the docs:

Eg. http://starterkit.test/api/pages/photography+animals/files/abba.jpg

With a request body like this

{
  "alt": "A beautiful mountain and lake"
}

mmh, i don’t see what i am doing wrong:

data: {
content: Object { user: "" }
dimensions: Object { width: 0, height: 0, ratio: 0, … }
exists: true
extension: "pdf"
filename: "acid-communism-intro-ch-1.pdf"
id: "course/module-01/acid-communism-intro-ch-1.pdf"
link: "/pages/course+module-01/files/acid-communism-intro-ch-1.pdf"
mime: "application/pdf"
modified: "2019-10-19T12:03:56+00:00"
name: "acid-communism-intro-ch-1"
next: null
niceSize: "82.77 kB"
options: Object { changeName: true, create: true, delete: true, … }
...
size: 84758
template: "assignment"
type: "document"
...
status: "ok"
}
xhr({
    method: 'patch',
    headers: {
      Authorization: `Basic ${opts.auth}`
    },
    uri: `/api/pages/${opts.page_id}/files/${opts.filename}`,
    body: { "user": "andre" }
  }, cb)
XHR patch https://mooc.test/api/pages/course+module-01/files/acid-communism-intro-ch-1.pdf
[HTTP/2.0 400 Bad Request 1ms]

Do PATCH request work in general apart from this example?

Can you modify file data using the API and a tool like Rested or Postman?

it works if i do it with curl :triumph:. thanks, must be some settings in my javascript code.

UPDATE

replacing this npm module with the new js Fetch API makes it work. not sure what changes and what i am misconfuring, but good for now.

UPDATE II

not sure what i did, i wrote the same code in the main view file, then copy-pasted it as a module in another file… it’s now working. NO IDEA :upside_down_face:

UPDATE III

  • the xhr call method must be typed all caps (patch => PATCH)
  • the body object must be json-stringify()