Results 1 to 5 of 5

Thread: Bug with WebRequest.post()

  1. #1

    Bug with WebRequest.post()

    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

    Code:
    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()

  2. #2
    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/ScriptRefer...uest.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/ScriptRefer...estHeader.html
    on that last overload it would be basically
    Code:
    // 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

  3. #3
    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.

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

  4. #4
    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/ref...rest/database/

  5. #5
    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.

    Quote Originally Posted by dzikakulka View Post
    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/ref...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.

Similar Threads

  1. WebRequest Params
    By Abarden in forum Scripting Suggestions
    Replies: 8
    Last Post: 09-25-2018, 09:02 PM
  2. [COMMUNITY] The Weekly Post #4
    By Kimiko in forum Announcements
    Replies: 1
    Last Post: 09-03-2015, 07:09 AM
  3. [COMMUNITY] The Weekly Post #3
    By Kimiko in forum Announcements
    Replies: 3
    Last Post: 08-26-2015, 04:44 PM
  4. [COMMUNITY] The Weekly Post #2
    By Kimiko in forum Announcements
    Replies: 5
    Last Post: 08-24-2015, 08:57 AM
  5. [COMMUNITY] The Weekly Post #1
    By Kimiko in forum Announcements
    Replies: 0
    Last Post: 08-12-2015, 12:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •