PDA

View Full Version : Bug with WebRequest.post()



Amuzet
07-23-2018, 01:55 PM
My first time using post() in any sort of way but it appears to be Bugged
if its been updated your documentation has yet to reflect it.
Error:"function doesn't match any overload" @Line 18


function callback(req)
if req.is_done and not req.is_error then
print('Got: ' .. req.text)
end
end

function onLoad()
local exampleData = {
identifiers = {
{ id = '683a5707-cddb-494d-9b41-51b4584ded69' },
{ name = 'Ancient Tomb' },
{ set = 'mrd',
collector_number = '150' },
},
pretty = true
}

WebRequest.post('https://api.scryfall.com/cards/collection', exampleData, callback)
end

Interestingly .put() works fine but the site im trying to .post() to wont take .put()

dzikakulka
07-23-2018, 02:44 PM
As I posted on Discord:

I think the problem is Unity sets some default headers fo forms, from the doc Knil linked
(https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Post.html)
The Content-Type header will be set to application/x-www-form-urlencoded.

and we can't change it while sites will often be sensitive to that (to handle different types of requests differently at the same URL).
We could use being able to change the Content-Type header, or even better if we were able to provide a similiar string-string dict for headers.

Here's the method to set a single header:
https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.SetRequestHeader.html
on that last overload it would be basically


// stuff
UnityWebRequest www = UnityWebRequest.Post(userUrl, userForm);
www.SetRequestHeader("Content-Type", userCTHeader)
// or even better (my C# is rusty but you get the idea)
foreach(var item in userHeaderDictionary)
{
www.SetRequestHeader(item.Key, item.Value);
}
yield return www.SendWebRequest();
// stuff

berserk3k
07-23-2018, 03:02 PM
I found .post will absolutely not allow associative arrays (or even more then 1 level deep normal arrays) so I been just combining all my "key=val;etc" into a string and sending it that way and fixing it on the receiver.


WebRequest.post(ns.user.url, {t = "/dc k=v;k=v;k=v"}, Global, 'dist');

dzikakulka
07-23-2018, 03:15 PM
Yeah, I forgot that, Knil mentioned that WebRequest.post only takes a table that has strings as keys and strings as values under them. You can JSON.encode more complex values there but still many sites will not treat our request as JSON data until the "Content-Type" header is set to "application/json", which is not the default.

Examples off the top of my history:
https://scryfall.com/docs/api/cards/collection
https://firebase.google.com/docs/reference/rest/database/

berserk3k
07-23-2018, 03:32 PM
One of the first things I tried when I started my (quasi-)client/server was WebRequest.setRequestHeader("X-Requested-With", ns.cuid .. "XMLHttpRequest"); and a few other header types to no avail.


Yeah, I forgot that, Knil mentioned that WebRequest.post only takes a table that has strings as keys and strings as values under them. You can JSON.encode more complex values there but still many sites will not treat our request as JSON data until the "Content-Type" header is set to "application/json", which is not the default.

Examples off the top of my history:
https://scryfall.com/docs/api/cards/collection
https://firebase.google.com/docs/reference/rest/database/

All right, I'm going to try that again! I did attempt to encode to just a json string and send that and decode it on the server but couldn't get it to work; TTS errors. Was a couple weeks ago now and just converting everything to a string now, like mentioned above (#3 post). Meh, it works for me as well as I need it too. Guess I'll keep an eye out for changes to this portion of the API.