Docker Community Forums

Share and learn in the Docker community.

API error while doing a network connect or disconnect

#1

I am trying to use the low level API to perform the network connect and disconnect operations. I am in python and I know I could use the docker module but I am also having an issue there (and I think the 2 are not related). So, for this low level API issue I did a very simple test where the container and network pre-exist and I simply connect / disconnect the container. In both cases I am getting the following error message:

<Response [500]>
{‘message’: ‘json: cannot unmarshal string into Go value of type types.NetworkDisconnect’}

Doing a simple List command (as in the code below) works perfectly.

I try to replace the network Id by the network name but get the same result (probably because the error happens before the daemon even looks at the Id…)(I got the Id from the docker network inspect CLI command)

My setup:

Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.4
Git commit: e68fc7a
Built: Thu Nov 15 21:12:47 2018
OS/Arch: linux/amd64
Experimental: false

Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.4
Git commit: e68fc7a
Built: Sun Nov 11 21:53:22 2018
OS/Arch: linux/amd64
Experimental: false

And this is the code:

import requests
import requests_unixsocket
import json

with requests_unixsocket.monkeypatch():
rList = requests.get(‘http+unix://%2Fvar%2Frun%2Fdocker.sock/networks’)
print (rList)
print(rList.json())

connectPayload = { 'Container':'uri_dav_1' }
connectPayloadJSON = json.dumps(connectPayload)
print (connectPayloadJSON)

rConnect = requests.post('http+unix://%2Fvar%2Frun%2Fdocker.sock/networks/23ddf50710e0604594b0062e841aeb0ea9c944b659d34e537f457da0d5a2431e/connect', json=connectPayloadJSON)
print (rConnect)
print(rConnect.json())

disconnectPayload = { 'Container':'uri_dav_1' }
disconnectPayloadJSON = json.dumps(disconnectPayload)
print (disconnectPayloadJSON)

rDisconnect = requests.post('http+unix://%2Fvar%2Frun%2Fdocker.sock/networks/23ddf50710e0604594b0062e841aeb0ea9c944b659d34e537f457da0d5a2431e/disconnect', json=disconnectPayloadJSON)
print (rDisconnect)
print(rDisconnect.json())