API error: "You don't have the permission to access the requested resource. It is either read-protected or not readable by the server."

Want to send gcode to the printer before shutdown. Using 'curl' I send the data to api/printer/command and getting the error:
"error": "You don't have the permission to access the requested resource. It is either read-protected or not readable by the server."

What did you already try to solve it?

In octoprint, Settings, Access Control, gave me access to everything.

Have you tried running in safe mode?

Yes, to no avail.

Did running in safe mode solve the problem?

No.

Whether I use command line or send json file, says the same thing.

json file:
POST http://127.0.0.1/api/printer/command HTTP/1.1
Host: 127.0.0.1
Content-Type: application/json
X-Api-Key: ABCDEFG... (My key)

{
"command": "M140 S20"
}

-JC

Not your problem, but isn't that supposed to be "M140 S20"???

1 Like

True. Thank you for bringing that to my attention.

-JC

1 Like

First - you’ve got a json file that doesn’t contain json. If it’s got those headers & stuff in it, it’s not json. JSON would be just the bit in curly brackets at the bottom.

-d is for sending data, not a file that curl will read and send. You’ve literally sent OctoPrint “@/etc/prntoff.json”, not the content of the file. The reason you get a “not authorised” error is that you never actually sent an API key. You can see it’s not there in the verbose output - everything with a > is being sent to the server.

I would refer to this post for an example - as it says, OctoPrint’s API is nothing special and any resource on how to use curl would help you.

1 Like

The information I am using is an example from the documentation. That is why I am using it.
Sending data from a file is '-d @[filename]. It is sending more than just the file name;

upload completely sent off: 191 out of 191 bytes

The API key I blanked out. Didn't want to give it out. That is correct.

The error of '403 forbidden' is what I cannot get passed.
I am pretty sure it's an authentication thing and I have tried user Id and password with the same results.

Was just wondering if there is something locked/disabled on the octoprint side.

-JC

None of that changes the fact that you can't send the headers with the data. It's clear from your output that the headers did not include the authentication information from the file.

The short version is: you need to include the authentication as either a header or a URL query parameter. The data param is data only.

Longer, technical detail: Curl sends the headers separated by newlines followed by 2 new lines (ie. a blank line) and then the data. So even though your data does technically include the header information first, the server is already done processing headers by the time it sees that.
Your verbose output also shows that you didn't set the content type to application/json, so once you the authentication working, you need to supply that header as well, as I showed in my example that Charlie linked.

Also, even if the above were not true, the Curl man page states:

Posting data from a file named 'foobar' would thus be done with -d, --data @foobar. When -d, --data is told to read from a file like that, carriage returns, newlines and null bytes are stripped out.

Which would cause headers to not be parsed properly since a server is expecting new lines between each.

1 Like

You know, to keep this 'mostly' inside OctoPrint, I would use Custom Control Editor and GCode Systems Commands plugins.
Using CCE, put a button in the Control Tab called "Shutdown" that runs the
M140 S0 ; shut off the bed
M106 S255 ; turn on the part cooler fan - really helps move the air near the hotend
M109 R50 ; set min temp of hotend
M106 S0 ; after cool kill the fan
OCTO100 ; call the 'replacemen' shutdown thru GCode System Command plugin

and in GCode System Command
OCTO100 set to "sudo shutdown -h now" or whatever...

I have tried many different ways. This the last one I tried;

sudo curl -v -d "X-Api-Key: AB0A2F0C4C8F4B068159373F5CB10566" --header 'Host: 127.0.1.1:80' -d @/etc/prntoff.json 'http://jct:pass1234@127.0.1.1/api/printer/command'

File:
{
"commands": [
"M140 S20",
"M109 R50"
]
}

Instead of what I am doing wrong, maybe fix what is here please.
-JC

Good point. I am already using the Gcode system commands plugin for something else.
I only see where I can add one button... am I missing something?

I just want to send a gcode command from the shutdown button;
image

Thanks,
-JC

Also I am having issues with the custom control editor... this is all it's showing.
image

BTW, I like the M106 255.
Didn't think about that one. Thanks.

Alright. If you heard a pop, it was me pulling my head out of my rear.
I got it to work. I was just making mistakes I should have picked up on.

Thank you everyone for the help.

If interested:
sudo curl -v -u ID:PASSWORD -H "X-Api-Key: AB0A2F0C4C8F4B068159373F5CB10566" -H "Content-Type: application/json" -H 'Host: 127.0.1.1:80' -d @/etc/prntoff.json 'http://127.0.1.1/api/printer/command'

-JC

1 Like

You do know almost every plugin has a configuration page in the settings popup?
Settings(wrench)>Plugins(way at the bottom)>[pluginname].

This is unfortunately (and I suspect we share this opinion) the daft way that custom controls shows when you have nothing configured.

Right click the green box to add your first control. Then you can edit it, right click to add more etc. It’s not explained well.

Glad you managed to get curl sorted :slightly_smiling_face:

Thank you sir!

Thanks for everything.

-JC

I did not know that. Thank you!

-JC

Some say I have gone too far...

I like! Been doing hardware/software automation for 30+ years... There is NEVER too much.