POST Requests via Python
-
kann mir jemand helfen - wenn ich z.B. via Insomnia den POST Request unten an https://idoit-test/src/jsonrpc.php schicke, dann klappt das mit dem Server anlegen.
{
"jsonrpc": "2.0",
"method": "cmdb.object.create",
"params": {
"type": "C__OBJTYPE__SERVER",
"title": "Testserver via API",
"apikey": "xxxx"
},
"id": 1
}wenn ich das gleiche mit dem Python Code unten versuche, passiert gar nichts
import urllib.parse
import urllib.requesturl = 'https://idoit-test/src/jsonrpc.php'
values = {'jsonrpc': '2.0', 'method': 'cmdb.object.create', 'params': {'type': 'C__OBJTYPE__SERVER', 'title': 'Testserver via API', 'apikey': 'xxxx'}, 'id': 1}data = urllib.parse.urlencode(values)
req = urllib.request.Request(url, data.encode('utf-8'))
response = urllib.request.urlopen(req)
the_page = response.read()GET Request via Python funktionieren problemlos.
-
Das URL-Encoding sieht für mich falsch aus. Ich würde mal etwas in die Richtung
import json import urllib.request url = 'https://idoit-test/src/jsonrpc.php' values = {'jsonrpc': '2.0', 'method': 'cmdb.object.create', 'params': {'type': 'C__OBJTYPE__SERVER', 'title': 'Testserver via API', 'apikey': 'xxxx'}, 'id': 1} req = urllib.request.Request(url, json.dumps(values).encode('ASCII')) response = urllib.request.urlopen(req) the_page = response.read()probieren.
-
danke für das Feedback. Deine Version läuft auch fehlerfrei durch, ein Objekt wieder aber leider nicht angelegt

-
Da fehlt wohl noch der
content-type: application/jsonheader.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login