Shared.SendHTTPRequest w/ "PUT" and distinct querystring and body?

WyzcrakWyzcrak Pot Pie Aficionado Join Date: 2002-12-04 Member: 10447Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue
Shared.SendHTTPRequest(url, "PUT", data, callback)

My 'url' input has a querystring (an auth token, per my target API's documentation). My 'data' input is a table meant to be sent in the body of the request (as serialized JSON).

Am I calling SendHTTPRequest correctly? Are my expectations correct? Is "PUT" an acceptable method (it's certainly correct for the service I'm calling -- that's not in question).

At runtime, the NS2 server seems to be appending the table's key/value pairs to the querystring instead of sending them in the request body. Also, it's adding a question mark to my 'url' input before appending those values, such that the final URL has two question marks (malformed URL, b/c my 'url' input already had a querystring).

Comments

  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    edited December 2013
    I havent used or read up on the http stuff in spark (so I cant say for sure exactly how it behaves, but my guess is that it sends a regular http request and returns the response?), but if you know what the service wants and should reply, you could always use wireshark or similar programs to make sure everything looks right (such as, that you actually send a http request).
    What is httprequest supposed to return for you (iirc that would be 200, 202 or 204 since you use PUT), and what does it return?
  • WyzcrakWyzcrak Pot Pie Aficionado Join Date: 2002-12-04 Member: 10447Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue
    I've verified at the destination service that the querystring I specify in my 'url' input is not the querystring being sent to the destination service. It's being changed in the way I described in my first post.
  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    edited December 2013
    Ah. So I take it you want the data to be sent as the content of the PUT request instead, rather than something appended to the url? Would make sense, considering that it is a PUT request (seems I didnt read your first post as well as I ought to have) :P

    By testing a few things and listening with wireshark, it appears as if sendhttprequest sends a GET request and not a PUT request, when using "PUT".

    I made a piece of code that does PUT, POST and GET, all with the same params. Wireshark shows 1 packet with GET being sent to my destination, then 1 POST, and then 1 GET again.
    The responses are, from the 2 GET requests, 2 identical "moved permanently" responses telling me to use http://www.google.com rather than google.com, and from the POST a "Method not allowed", which was to be expected.
    The "moved permanently" results in two new GET requests to the correct place, which then returns two "200 OK" responses, together with googles html data.
    The callbacks then print to the console:
    POST: 1 ; value = <!DOCTYPE html>
    <html lang=en>
    <meta charset=utf-8>
    <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
    <title>Error 405 (Method Not Allowed)!!1</title>
    <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
    </style>
    <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
    <p><b>405.</b> <ins>That’s an error.</ins>
    <p>The request method <code>POST</code> is inappropriate for the URL <code>/</code>. <ins>That’s all we know.</ins>
    PUT: 1 ; value = <!doctype html>[loads of html]
    GET: 1 ; value = <!doctype html>[loads of html]

    The html differs slightly for PUT and GET, but to see how, you would simply have to run the same code as I posted below.


    So my conclusion would be that PUT simply does not work as intended, and ends up sending a GET request instead.


    Here is the code I used to test:
    http://pastebin.com/HqnmYGVx
  • WyzcrakWyzcrak Pot Pie Aficionado Join Date: 2002-12-04 Member: 10447Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue
    Ah. Yes. I missed in my initial testing that the NS2 server is changing my specified "PUT" method value to "GET". I've tested again just now and my observations match yours.

    Thank you for your thorough tests.

    Hopefully this thread will help someone else in the future.

    If "PUT" is not to be a valid method value, I'd love to see the documentation changed to express that.
  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    I would love to see any documentation to be made at all. The page in the wiki does not even have that generic page made by a bot, its simply nonexistant. Very few things does tbh, and I generally look it up using find in files...

    I'm interested to know, how did you solve it in the end? Did you make your server listen for GET instead, and retrieve the data from the key-value pairs in the url, or did you come up with something cooler?
  • WyzcrakWyzcrak Pot Pie Aficionado Join Date: 2002-12-04 Member: 10447Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue
    I was reading the documentation in C:\Program Files (x86)\Steam\steamapps\common\Natural Selection 2\docs\api\Shared.json.

    I didn't solve the problem in the end. The destination service isn't mine to modify.

  • FehaFeha Join Date: 2006-11-16 Member: 58633Members
    I am unsure if this would actually work reliably, but wouldnt it be possible to have spark send a GET/POST request to an intermediary server which translates it to a PUT and sends it to the destination service?
    Well, provided you ignore the response then it would be pretty simple I think, but what I meant with being unsure, was that I dont know if you can reliably hold the response until you could fill it with the response of that other server.
  • WyzcrakWyzcrak Pot Pie Aficionado Join Date: 2002-12-04 Member: 10447Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue
    Authoring a middle-man proxy would be fairly simple. I don't have any plans to do that.
Sign In or Register to comment.