Community
    • Categories
    • Recent
    • Popular
    • Users
    • Search
    • Register
    • Login

    POST Requests via Python

    Scheduled Pinned Locked Moved Entwicklung
    4 Posts 2 Posters 552 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      andik
      last edited by

      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.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}

      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.

      1 Reply Last reply Reply Quote 0
      • F Offline
        franknagel
        last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • A Offline
          andik
          last edited by

          danke für das Feedback. Deine Version läuft auch fehlerfrei durch, ein Objekt wieder aber leider nicht angelegt 😞

          1 Reply Last reply Reply Quote 0
          • F Offline
            franknagel
            last edited by

            Da fehlt wohl noch der content-type: application/json header.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post