RESTful API won't let me rest, getting 405 response

Status
Not open for further replies.
Joined
Nov 4, 2016
Messages
2
First off I’d like to say that I’ve enjoyed using FreeNAS and I think it’s a great piece of software.

I apologize in advance if this post isn’t in the correct area, I didn’t see a topic for the API.

Unfortunately I’ve run in to a snag using the RESTful API and I hope someone here can help or at least shed some light on my dilemma.

I’m developing in Java using Jersey 2.21, trying to make a call to the shutdown resource (http://api.freenas.org/resources/system.html#shutdown) however I’m receiving a 405 Method not allowed. I tried making the same request using SOAPUI and everything worked fine so I’m pretty sure the problem is in my code. After trying a number of different things in code I finally broke out WireShark and captured the packets to see what was really happening. This is where I’m hoping someone here can help.

I'm using FreeNAS build 9.10.1-U2 (f045a8b)

Below are the first 2 packets (for reference) which successfully call the shutdown resource and the response 202 from the server. I’ve redacted the auth but it’s correct and identical in both failed and successful packets.

Code:
Successful Request

0000  bc 5f f4 8b c1 b5 00 25 22 dc 48 c4 08 00 45 00  ._.....%".H...E.
0010  01 34 3c 3a 40 00 80 06 00 00 c0 a8 64 78 c0 a8  .4<:@.......dx..
0020  64 fa 0e e4 00 50 e6 05 3f a5 ca f9 eb 54 50 18  d....P..?....TP.
0030  00 ff 4b ea 00 00 50 4f 53 54 20 2f 61 70 69 2f  ..K...POST /api/
0040  76 31 2e 30 2f 73 79 73 74 65 6d 2f 73 68 75 74  v1.0/system/shut
0050  64 6f 77 6e 2f 20 48 54 54 50 2f 31 2e 31 0d 0a  down/ HTTP/1.1..
0060  41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e 67 3a  Accept-Encoding:
0070  20 67 7a 69 70 2c 64 65 66 6c 61 74 65 0d 0a 43  gzip,deflate..C
0080  6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 70  ontent-Type: app
0090  6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 0d 0a 43  lication/json..C
00a0  6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 30  ontent-Length: 0
00b0  0d 0a 48 6f 73 74 3a 20 31 39 32 2e 31 36 38 2e  ..Host: 192.168.
00c0  31 30 30 2e 32 35 30 0d 0a 43 6f 6e 6e 65 63 74  100.250..Connect
00d0  69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69 76 65 0d  ion: Keep-Alive.
00e0  0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 41 70 61  .User-Agent: Apa
00f0  63 68 65 2d 48 74 74 70 43 6c 69 65 6e 74 2f 34  che-HttpClient/4
0100  2e 31 2e 31 20 28 6a 61 76 61 20 31 2e 35 29 0d  .1.1 (java 1.5).
0110  0a 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 3a 20  .Authorization:
0120  42 61 73 69 63 20 XX XX XX XX XX XX XX XX XX XX  Basic xxxxxxxxxx
0130  XX XX XX XX XX XX XX XX XX XX XX XX XX XX 0d 0a  xxxxxxxxxxxxxx..
0140  0d 0a  ..


Code:
Successful Response

0000  00 25 22 dc 48 c4 bc 5f f4 8b c1 b5 08 00 45 00  .%".H.._......E.
0010  01 38 00 b6 40 00 40 06 ee 46 c0 a8 64 fa c0 a8  .8..@.@..F..d...
0020  64 78 00 50 0e e4 ca f9 eb 54 e6 05 40 b1 50 18  dx.P.....T..@.P.
0030  04 02 5d df 00 00 48 54 54 50 2f 31 2e 31 20 32  ..]...HTTP/1.1 2
0040  30 32 20 41 43 43 45 50 54 45 44 0d 0a 53 65 72  02 ACCEPTED..Ser
0050  76 65 72 3a 20 6e 67 69 6e 78 2f 31 2e 38 2e 31  ver: nginx/1.8.1
0060  0d 0a 44 61 74 65 3a 20 46 72 69 2c 20 30 34 20  ..Date: Fri, 04
0070  4e 6f 76 20 32 30 31 36 20 31 38 3a 34 32 3a 32  Nov 2016 18:42:2
0080  36 20 47 4d 54 0d 0a 43 6f 6e 74 65 6e 74 2d 54  6 GMT..Content-T
0090  79 70 65 3a 20 74 65 78 74 2f 68 74 6d 6c 3b 20  ype: text/html;
00a0  63 68 61 72 73 65 74 3d 75 74 66 2d 38 0d 0a 54  charset=utf-8..T
00b0  72 61 6e 73 66 65 72 2d 45 6e 63 6f 64 69 6e 67  ransfer-Encoding
00c0  3a 20 63 68 75 6e 6b 65 64 0d 0a 43 6f 6e 6e 65  : chunked..Conne
00d0  63 74 69 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69 76  ction: keep-aliv
00e0  65 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 61 6e 67 75  e..Content-Langu
00f0  61 67 65 3a 20 65 6e 0d 0a 56 61 72 79 3a 20 41  age: en..Vary: A
0100  63 63 65 70 74 2c 20 41 63 63 65 70 74 2d 4c 61  ccept, Accept-La
0110  6e 67 75 61 67 65 2c 20 43 6f 6f 6b 69 65 0d 0a  nguage, Cookie..
0120  0d 0a 31 39 0d 0a 53 68 75 74 64 6f 77 6e 20 70  ..19..Shutdown p
0130  72 6f 63 65 73 73 20 73 74 61 72 74 65 64 2e 0d  rocess started..
0140  0a 30 0d 0a 0d 0a  .0....


Here’s the problem child below… What seems very odd to me is that in the 405 response it says that GET is allowed. Not only is that not what’s in the documentation but using the GET method doesn’t work either. Additionally, we already saw POST correctly work. The ordering of some of the elements aren’t the same (though I’m pretty sure I can’t control that, nor does it matter.)

Code:
Failed Request

0000  bc 5f f4 8b c1 b5 f8 ca b8 4a 1f 8d 08 00 45 00  ._.......J....E.
0010  01 78 79 39 40 00 80 06 00 00 c0 a8 64 7d c0 a8  .xy9@.......d}..
0020  64 fa 26 ec 00 50 06 a9 52 90 ad c6 53 54 50 18  d.&..P..R...STP.
0030  08 05 4c 33 00 00 50 4f 53 54 20 2f 61 70 69 2f  ..L3..POST /api/
0040  76 31 2e 30 2f 73 79 73 74 65 6d 2f 73 68 75 74  v1.0/system/shut
0050  64 6f 77 6e 2f 20 48 54 54 50 2f 31 2e 31 0d 0a  down/ HTTP/1.1..
0060  41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e 67 3a  Accept-Encoding:
0070  20 67 7a 69 70 2c 64 65 66 6c 61 74 65 0d 0a 43  gzip,deflate..C
0080  6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 70  ontent-Type: app
0090  6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 0d 0a 41  lication/json..A
00a0  75 74 68 6f 72 69 7a 61 74 69 6f 6e 3a 20 42 41  uthorization: BA
00b0  53 49 43 20 XX XX XX XX XX XX XX XX XX XX XX XX  SIC xxxxxxxxxxxx
00c0  XX XX XX XX XX XX XX XX XX XX XX XX 0d 0a 55 73  xxxxxxxxxxxx..Us
00d0  65 72 2d 41 67 65 6e 74 3a 20 4a 65 72 73 65 79  er-Agent: Jersey
00e0  2f 32 2e 32 31 20 28 48 74 74 70 55 72 6c 43 6f  /2.21 (HttpUrlCo
00f0  6e 6e 65 63 74 69 6f 6e 20 31 2e 38 2e 30 5f 37  nnection 1.8.0_7
0100  33 29 0d 0a 48 6f 73 74 3a 20 31 39 32 2e 31 36  3)..Host: 192.16
0110  38 2e 31 30 30 2e 32 35 30 0d 0a 41 63 63 65 70  8.100.250..Accep
0120  74 3a 20 74 65 78 74 2f 68 74 6d 6c 2c 20 69 6d  t: text/html, im
0130  61 67 65 2f 67 69 66 2c 20 69 6d 61 67 65 2f 6a  age/gif, image/j
0140  70 65 67 2c 20 2a 3b 20 71 3d 2e 32 2c 20 2a 2f  peg, *; q=.2, */
0150  2a 3b 20 71 3d 2e 32 0d 0a 43 6f 6e 6e 65 63 74  *; q=.2..Connect
0160  69 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69 76 65 0d  ion: keep-alive.
0170  0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a  .Content-Length:
0180  20 30 0d 0a 0d 0a  0....


Code:
Failed Response

0000  f8 ca b8 4a 1f 8d bc 5f f4 8b c1 b5 08 00 45 00  ...J..._......E.
0010  01 2f 01 d0 40 00 40 06 ed 30 c0 a8 64 fa c0 a8  ./..@.@..0..d...
0020  64 7d 00 50 26 ec ad c6 53 54 06 a9 53 e0 50 18  d}.P&...ST..S.P.
0030  04 02 26 7e 00 00 48 54 54 50 2f 31 2e 31 20 34  ..&~..HTTP/1.1 4
0040  30 35 20 4d 45 54 48 4f 44 20 4e 4f 54 20 41 4c  05 METHOD NOT AL
0050  4c 4f 57 45 44 0d 0a 53 65 72 76 65 72 3a 20 6e  LOWED..Server: n
0060  67 69 6e 78 2f 31 2e 38 2e 31 0d 0a 44 61 74 65  ginx/1.8.1..Date
0070  3a 20 46 72 69 2c 20 30 34 20 4e 6f 76 20 32 30  : Fri, 04 Nov 20
0080  31 36 20 31 39 3a 33 34 3a 34 34 20 47 4d 54 0d  16 19:34:44 GMT.
0090  0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74  .Content-Type: t
00a0  65 78 74 2f 68 74 6d 6c 3b 20 63 68 61 72 73 65  ext/html; charse
00b0  74 3d 75 74 66 2d 38 0d 0a 54 72 61 6e 73 66 65  t=utf-8..Transfe
00c0  72 2d 45 6e 63 6f 64 69 6e 67 3a 20 63 68 75 6e  r-Encoding: chun
00d0  6b 65 64 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a  ked..Connection:
00e0  20 6b 65 65 70 2d 61 6c 69 76 65 0d 0a 41 6c 6c  keep-alive..All
00f0  6f 77 3a 20 47 45 54 0d 0a 43 6f 6e 74 65 6e 74  ow: GET..Content
0100  2d 4c 61 6e 67 75 61 67 65 3a 20 65 6e 0d 0a 56  -Language: en..V
0110  61 72 79 3a 20 41 63 63 65 70 74 2d 4c 61 6e 67  ary: Accept-Lang
0120  75 61 67 65 2c 20 43 6f 6f 6b 69 65 0d 0a 0d 0a  uage, Cookie....
0130  33 0d 0a 47 45 54 0d 0a 30 0d 0a 0d 0a  3..GET..0....


What is the API expecting that exists in the first request but not the second? Also, why is the response 405 and saying GET is allowed? Please help, it’s greatly appreciated!

P.S. If there are any JavaDevs in the house I can post the code I'm using.
 

SweetAndLow

Sweet'NASty
Joined
Nov 6, 2013
Messages
6,421
Not using java but this is how you shut it down using python request:
Code:
import requests
from requests.auth import HTTPBasicAuth
r = requests.post('http://<freenas IP>/api/v1.0/system/shutdown/', auth=HTTPBasicAuth('root', 'pass'))
>>> print r
<Response [202]>


Here is an example using curl
Code:
curl -X POST -u root:pass http://<freenas IP>/api/v1.0/system/shutdown/


How every you do this is java should be exactly the same.

EDIT: hmm i looked over your post again and I don't think my answer helped you. I have no clue why you are getting a 405 error. I would post some code snippets if you can.
 
Joined
Nov 4, 2016
Messages
2
Thanks for your response, but that unfortunately doesn't do me any good. While I'd like to get my code correct, for right now I'm just trying to understand why the "failed" request got a 405.

With that in mind, here's the Java I'm using. Note that both the json() and request() methods contain empty strings. I've tried using null and populating them but received the same result. Additionally, I've tried turning redirects on. They're currently set to off because if a POST method is made and receives a 3xx response it will not perform a POST on the new location but rather a GET which results in a subsequent 4xx response.

Code:
clientURI = "http://" + Properties.getClientIP() + "/api/v1.0/";
ClientConfig config = new ClientConfig().property(ClientProperties.FOLLOW_REDIRECTS, false);
client = ClientBuilder.newClient(config);
client.register(new ClientAuthenticator(Properties.getClientUsername(), Properties.getClientPassword()));
Builder builder = client.target(clientURI + "system/shutdown/").request("");
builder.acceptEncoding("gzip", "deflate");
response = builder.post(Entity.json(""));
 
Status
Not open for further replies.
Top