Can't attach PDF Documents

Help please!

I cannot attach documents via the API in v5.

Documentation says to format attaching documents as follows:
curl -X POST https://invoicing.co/api/v1/invoices/<invoice_id>
-H ‘Content-Type: multipart/form-data’
-H ‘X-API-TOKEN: TOKEN’
-H ‘X-Requested-With: XMLHttpRequest’
-F _method=PUT
-F ‘documents[]=@filename.png’

I need to do this via python and have the following code.

import requests

invoice_id = 'pmbkJqAvaz'
file = 'https://pdf-temp-files.s3.amazonaws.com/c4614a7a83de4972a1a5e5adad1668f8/time_sheet.pdf'

headers = {
    'X-API-TOKEN': '7KZ09cUvsfdeVpMraynSCJEzuOOqkambGTKdyUZR4zTSMtzfbJWBS7KPgGCd6NBc',
    'X-Requested-With': 'XMLHttpRequest',
}

files = {
    '_method': (None, 'PUT'),
    'documents[]': file
}

response = requests.post('https://invoicing.co/api/v1/invoices/'+invoice_id, headers=headers, files=files)

response.status_code

I get a 200 response code and see that the document is attached to the invoice
Screen Shot 2021-12-04 at 5.37.35 PM

HOWEVER when I try to view the documents, the only thing that opens is a text page showing the URL
https://invoicing.co/documents/3uDs4xJ154Av8eZl1poR9udHL7OUTCa91uJ9cpHc.txt?inline=true

I know that I’m missing the ‘Content-Type’:‘multipart/form-data’ header, but every time I try to pass it in I get the following error ‘{“message”:“Method not supported for this route”}’

I also can’t manually upload documents at all, as every time I click the button it doesn’t do anything.

What am I doing wrong here?

Hi,

@david can you spot any problems?

which docs are you looking at?

from my reference i see the following should work

curl -X POST
http://ninja.test:8000/api/v1/invoices/1YQdJ2dOGp/upload
-H ‘Content-Type: multipart/form-data’
-H ‘X-API-TOKEN: token’
-H ‘X-Requested-With: XMLHttpRequest’
-F _method=PUT
-F documents=@/home/david/settings.txt;

@david Docs I’m referencing are Free Source Available Invoicing, Expenses & Time-Tracking | Invoice Ninja

The example given in the docs for attaching invoices is also different from what you are sharing here. You say that your code “should work” but I really need a verified example.

Are you able to provide me the true working example of how to attach documents? I keep trying all different variations and nothing seems to work.

@zmccarty56

The example in the docs is missing the /upload in the route, i’ll adjust this now, i’ve tested the following as working correctly.

curl -X POST 'http://ninja.test:8000/api/v1/invoices/l4zbq2dprO/upload' \
-H 'Content-Type: multipart/form-data' \
-H 'X-API-TOKEN: company-token-test' \
-H 'X-Requested-With: XMLHttpRequest' \
-F _method=PUT \
-F documents[]=@/home/david/upload.txt;

Can you confirm that the URL should be

https://invoicing.co/api/v1/invoices/<invoice_id>/upload

I noticed you have http in your example

yes, for our hosted platform, you would use

https://invoicing.co/{invoice_id}/upload

So the url doesn’t need /api/vi/invoices???

I’m very confused, as the working example you showed me has a full url containing /api/invoices/<invoice_id>

But then you followed up and didn’t include those values? What is the correct url for a v5 document upload on hosted.

curl -X POST https://invoicing.co/api/v1/invoices/<invoice_id>/upload \
  -H 'Content-Type: multipart/form-data' \
  -H 'X-API-TOKEN: TOKEN' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -F _method=PUT \
  -F 'documents[]=@filename.png'

This used to work fine using zapier in v4. Why can I not get this working at all even while using curl in v5?

I got the command working in curl just fine, but I need to convert it into a python request.

Do you have the ability to help me do this? Or can point me in the right direction to a resource? I’m trying to upload the document that I don’t have stored locally as it’s from a url

import requests

headers = {
    'Content-Type': 'multipart/form-data',
    'X-API-TOKEN': 'WLEyIKsii8JHPPOY4MGBEuEzjDY15A8Gcg71DXPraodGtahgtrMXL7HYG2C40Lzj',
    'X-Requested-With': 'XMLHttpRequest',
}

files = {
    '_method': (None,'PUT'),
    'documents[]': 'https://pdf-temp-files.s3.amazonaws.com/ae7f9005db69425a874e5b2e003b9e59/time_sheet.pdf'
}

response = requests.post('https://invoicing.co/api/v1/invoices/pnelKrqVdKz/upload', headers=headers, files=files)

response.status_code

response.json()

What response are you getting back from the server?

Also, are you able to reference a remote file that that in the request?

@david I’m getting back “Method not supported for this route”

@zmccarty

can you try changing requests.post to requests.put

New error I’m getting is {‘message’: ‘No query results for model [App\Models\Invoice].’}

@david

Here is where my code is at

import requests
        
headers = {
    'Content-Type': 'multipart/form-data',
    'X-API-TOKEN': 'WLEyIKsii8JHPPOY4MGBEuEzjDY15A8Gcg71DXPraodGtahgtrMXL7HYG2C40Lzj',
    'X-Requested-With': 'XMLHttpRequest',
}

# Uploaded document name
remote_name = 'https://pdf-temp-files.s3.amazonaws.com/ae7f9005db69425a874e5b2e003b9e59/time_sheet.pdf'    
# Name/path of the document on your local machine
local_name = "time_sheet.pdf"
# Now the put data, `open` the local file.
files = [ ("file", (remote_name, open(local_name, "rb"), "application/pdf")), ]
 
response = requests.post('https://invoicing.co/api/v1/invoices/pnelKrqVdKz/upload', headers=headers, files=files)

response.status_code

response.json()

One thing I’m concerned about is even if I exclude the document entirely and just try to run a post request with headers and url, I still get the Method not supported for this route error

So there is something outside of the file that isn’t working

Are you sure that Invoice exists?