Shared.SendHTTPRequest w/ "PUT" and distinct querystring and body?
Wyzcrak
Pot Pie Aficionado Join Date: 2002-12-04 Member: 10447Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue
in Modding
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).
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
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?
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:
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
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.
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?
I didn't solve the problem in the end. The destination service isn't mine to modify.
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.