0

Ok, first thing: I am not responsible for this code. I am dealing with a router. This router has a web interface, and the code I'm about to show is in the router's web interface. My job is to build an app to configure this router. And I need my app to perform the login operation in the router.

The thing is: the developers that created the web interface for this router, made the router accept a post request in which the body of the request is xml, and the content type is application/x-www-form-urlencoded !!!! (whaaaaaat???)

request being sent

The source code that generates this request is in the page:

function PostXML_log(pUrl, value) {
    $.ajax({
        type: "POST",
        dataType: 'xml',
        url: pUrl,
        processData: false,
        data: value,
        async: true,
        beforeSend: function () { show_message("wait"); },
        success: function (data) {
            var code = $(data).find('CODE').text();
            if (code == "0" || code == "-1") {
                $.cookie.json = false;
                $.cookie("stork", $(data).find('MESSAGE').text(), { expires: 1 });
                if (code == "-1")
                    window.location = "mac_err.html";
                else
                    location.href = "../main_status.html";
            }
            else show_message("error", err_message)
        },
        error: function () { show_message("error", 'loading Error...'); },
        timeout: 20000
    });

}

function loginIn() {
    var username = 'admin';//$('#username').val();
    var password = ASCII2HEX($('#login_pwd').val());
    var xml = '<?xml version="1.0" encoding="utf-8"?><LOGIN><USER_NAME>' + username + '</USER_NAME><PASSWD>' + password + '</PASSWD></LOGIN>';
    PostXML_log("/cgi-bin/cgi.cgi?/app/route/login", xml);
};

The problem is that I can't find a way to replicate this behavior using Postman. I couldn't find a way to send a body and use application/x-www-form-urlencoded as content type at the same time.

I tried 4 different strategies with no success:

1 - Send as body, using content type application/xml, results in no response being received (send as pure text or text/xml results the same):

enter image description here

2 - Send xml as form key, results in error

enter image description here

3 - Send xml as form value, results in error

enter image description here

4 - send xml as both key and value, error: (i tried putting the xml's <xml> tag as key and the <login> tag as value, as well as putting the whole xml as key and value, no luck)

enter image description here

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 1
    form-urlencoded data implies sending key/value pairs. You probably need to find out what key name is used for the xml string on receiving end – charlietfl Feb 09 '19 at 13:39
  • no key name is being used, as pointed by what is logged in the browser. I posted a print screen in the question showing this behavior –  Feb 09 '19 at 14:52
  • So are you saying the `$.ajax` does work? If it does you should be able to inspect the actual request in browser dev tools network and inspect headers, body etc – charlietfl Feb 09 '19 at 15:31
  • yes, the ajax does work. As I said, I posted a print screen of the request in the question –  Feb 09 '19 at 16:11
  • Perhaps the cookies are required? Also take a look at the "view URL encoded" request body, perhaps it has clues – charlietfl Feb 09 '19 at 16:15
  • No, this is the login form. No cookie is expected to be set. Also, the web interface works without any cookie previously set, and the request does not set any cookie as well –  Feb 09 '19 at 16:16
  • Also, the question is: How can I mimic this behavior using postman? –  Feb 09 '19 at 16:18
  • Perhaps needs same referrer? Can't see what is set as headers in postman images – charlietfl Feb 09 '19 at 16:21
  • As mentioned above all I can suggest is duplicating headers from working request – charlietfl Feb 09 '19 at 16:23
  • That's exactly what I'm asking: how to replicate the request headers in postman? –  Feb 09 '19 at 16:32
  • To see the postman images, you need to click in the image, then apply zoom –  Feb 09 '19 at 16:33
  • https://stackoverflow.com/questions/47295675/how-do-i-post-xml-data-to-a-webservice-with-postman – Amer Almadany Apr 06 '19 at 10:54
  • @AmerAlmadany this is not what I'm asking –  Apr 08 '19 at 16:12
  • i know this is from forever ago @user3303864, but in your samples your data isn't actually encoded. if it were you'd have `%3C` instead of `<` (for example). my understanding of `application/x-www-form-urlencoded` is that the caller is responsible for encoding prior to putting it on the wire. – Matt Felzani Feb 11 '22 at 18:09

0 Answers0