Ad Widget

Collapse

Script-Item: HttpRequest.post fails with 404

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DEV2DEV
    Junior Member
    • Apr 2024
    • 8

    #1

    Script-Item: HttpRequest.post fails with 404

    Hi all,
    I'm new to this forum. So please be patient.

    I'm creating some checks for an AVM Fritz!Box.
    For some of them I have to deal with SOAP-XML.

    This is a simplified check (at least the starting point):

    Code:
    var params = JSON.parse(value);
    
    var requestBody = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:InitChallenge xmlns:h="http://soap-authentication.org/digest/2001/10/" s:mustUnderstand="1"><UserID>admin</UserID></h:InitChallenge></s:Header><s:Body><u:GetForwardNumberOfEntries xmlns:u="urn:dslforum-org:service:Layer3Forwarding:1" /></s:Body></s:Envelope>';
    
    // Initialize challenge with Fritz!Box
    var url = 'http://192.168.178.1:49000/upnp/control/layer3forwarding';
    var request = new HttpRequest();
    request.addHeader('Content-Type', 'text/xml; charset="utf-8"');
    request.addHeader('SoapAction', 'urn:dslforum-org:service:Layer3Forwarding:1#GetForwardNumberOfEntries');
    var response = request.post(url, requestBody);
    
    return JSON.stringify(response);
    Running in Postman it works as expected. Same with JS in ioBroker.
    But in my Script-Item I alway get a '404 - NOT FOUND'.

    For me it seems that there's a problem with POST on a HttpRequest.
    GET (with of course a different endpoint) works fine.

    Any suggestions?

    Best regards
    Bernd
  • Answer selected by DEV2DEV at 12-04-2024, 21:11.
    markosa
    Senior Member
    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
    • Aug 2022
    • 104

    How about setting headers like this:
    Code:
    request.addHeader('Content-Type: multipart/form-data');
    request.addHeader('Authorization: Basic ' + btoa(Itop.params.user + ':' + Itop.params.password))
    Those are from iTop mediatype and that syntax work in our system.

    Comment

    • DEV2DEV
      Junior Member
      • Apr 2024
      • 8

      #2
      By the way:
      My Zabbix 6.4.12 is running as a Docker-Container on Synology in bridge-mode.
      I've already tried to map the used port (49000) to the container but with no success.

      Comment

      • DEV2DEV
        Junior Member
        • Apr 2024
        • 8

        #3
        In the meantime I've created a little test service that's reacting on GET and POST on the same endpoint with the same port (49000).
        Result:
        It's not Zabbix that's producing the problem!
        Seems that it's rather the Fritz!Box itself.

        Any ideas what's wrong with my request?
        I've already added an User-Agent but with no progress.

        Comment

        • Brambo
          Senior Member
          • Jul 2023
          • 245

          #4
          Are you sure you do a post and not an update towards your device with postman?
          Check the httprequest options and also the log functions to test your script:


          bonus tip: wireshark your postman request and check if maybe your password (not sure if you use in your script) is send over in Base64 or something else, you request http so not that hard to find out
          Last edited by Brambo; 12-04-2024, 09:17.

          Comment

          • DEV2DEV
            Junior Member
            • Apr 2024
            • 8

            #5
            Originally posted by Brambo
            Are you sure you do a post and not an update towards your device with postman?
            Yes - definitely!

            Click image for larger version  Name:	image.png Views:	1 Size:	96.1 KB ID:	482331

            When I create a HTTP-Agent item with POST and the exact same settings, the API works as expected.
            But a HTTP-Agent is not a choice because the API needs a two-step process.
            Question is: What's the difference between the POST of the HTTP-Agent item and the POST in my script?


            Here's the result with the HTTP-Agent:
            Click image for larger version

Name:	image.png
Views:	600
Size:	97.0 KB
ID:	482333
            Last edited by DEV2DEV; 12-04-2024, 09:36.

            Comment

            • Brambo
              Senior Member
              • Jul 2023
              • 245

              #6
              Still what are you really sending when you look at the package with wireshark. Postman is showing a lot but it is processed data.
              For example maybe you should not format the xml and just send it as a long string (just an example but you need to make sure how it's actually send)

              Comment

              • DEV2DEV
                Junior Member
                • Apr 2024
                • 8

                #7
                Originally posted by Brambo
                Still what are you really sending when you look at the package with wireshark. Postman is showing a lot but it is processed data.
                Postman is exactly sending what I would expect. Not more or less.

                Click image for larger version

Name:	image.png
Views:	749
Size:	60.8 KB
ID:	482357
                'Wiresharking' Zabbix wil get a little bit complicated, as Zabbix is running as a Docker-Container on Synology.
                Maybe I can extend my test-service to check the headers sent by Zabbix.

                For example maybe you should not format the xml and just send it as a long string (just an example but you need to make sure how it's actually send)
                I've already tried this, With no success.
                I think it' s not the content producing the error. When sending in Postman with a misformatted xml I get e.g. '401 - Unauthorized' but no 404.

                I can reproduce the 404 in Postman when I rename or delete the header 'SoapAction'.
                Seems that the HttpRequest does not send the headers or not in the required format.

                I've tried to not send the request but to read the request-object. It's empty
                I expexted to see the headers at least.

                Comment

                • DEV2DEV
                  Junior Member
                  • Apr 2024
                  • 8

                  #8
                  Originally posted by DEV2DEV
                  Maybe I can extend my test-service to check the headers sent by Zabbix.
                  With this code
                  Code:
                  const params = JSON.parse(value);
                  
                  const requestBody = '<?xml version="1.0" encoding="utf-8"?>' +
                  '<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">' +
                  '    <s:Header>' +
                  '        <h:InitChallenge xmlns:h="http://soap-authentication.org/digest/2001/10/" s:mustUnderstand="1">' +
                  '            <UserID>' + params.username + '</UserID>' +
                  '        </h:InitChallenge>' +
                  '    </s:Header>' +
                  '    <s:Body>' +
                  '        <u:' + params.action + ' xmlns:u="' + params.urn + '" />' +
                  '    </s:Body>' +
                  '</s:Envelope>';
                  
                  
                  const action = params.urn + '#' + params.action;
                  
                  // Initialize challenge with Fritz!Box
                  var url = 'http://' + params.ip + ':49000' + params.url;
                  const request = new HttpRequest();
                  request.addHeader('Content-Type', 'text/xml; charset="utf-8"');
                  request.addHeader('User-Agent', 'AVM UPnP/1.0 Client 1.0');
                  request.addHeader('SoapAction', action);
                  var response = request.post(url, requestBody);
                  
                  return JSON.stringify(response);
                  I set the header 'User-Agent'.
                  But my test-service does not receive it. With Postman it does.

                  Seems to me that Zabbix does not handle HttpRequest.setHeader() correctly

                  Comment

                  • markosa
                    Senior Member
                    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
                    • Aug 2022
                    • 104

                    #9
                    How about setting headers like this:
                    Code:
                    request.addHeader('Content-Type: multipart/form-data');
                    request.addHeader('Authorization: Basic ' + btoa(Itop.params.user + ':' + Itop.params.password))
                    Those are from iTop mediatype and that syntax work in our system.

                    Comment

                    • DEV2DEV
                      Junior Member
                      • Apr 2024
                      • 8

                      #10
                      Originally posted by markosa
                      How about setting headers like this ...
                      Hero of the day! That's it.

                      Never minded this because there has been no error using two parameters.

                      Click image for larger version

Name:	grafik.png
Views:	586
Size:	71.2 KB
ID:	482387

                      Comment

                      • Brambo
                        Senior Member
                        • Jul 2023
                        • 245

                        #11
                        You are now escaping the " with \" in your test item value.
                        So that could be the case, with the correct headers did you try also something like read out a value first check if the header part is correct etc etc do it step by step

                        Comment


                        • DEV2DEV
                          DEV2DEV commented
                          Editing a comment
                          This has only been a first test.
                      Working...