Search Unity

Resolved How do I make a UnityWebRequest Post call

Discussion in 'Visual Scripting' started by apiotuch_unity, Nov 19, 2022.

  1. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    When I try to make an HTTP Post UnityWebRequest, it fails on Wait While node. With a get request it is fine, but with post if fails. My VisualScript is a nested subgraph that uses input as an entrypoint. Error says It says it is an invalid coroutine. I tried using IsDone, Upload In Progress, and HTTPContinue, all cause an error when used as a condition for the while wait node.

    This is the error I get at the Wait While node:

    InvalidOperationException: Port 'enter' on 'WaitWhileUnit#9f85e...' can only be triggered in a coroutine


    I tried all the Wait coroutine nodes. All throw a similar error. I am using a SendWebRequest Node and I pass a url data Post node (but I only provide a url value).

    I do see that the node inspector mentions the Send Web Request is async, but my get subgraph works the same way and that doesn't fail at the wait node. So I imagine async/coroutine is abstracted and I am assuming the description is vague and inaccurate/out of date; seeing as I get different behavior simply between Post and Get web requests.
     
  2. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    I am using Unity 2021.3.11f1 with VisualScripting 1.7.8.
     
  3. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    I believe I figured it out using an UnityWebRequest Async Operation. I had to add that to the types list and regen the nodes., Then hooking it up to the send WebRequest node set it up for me to check using an update node to see when it was finished. But right now I am storing the async operation object as a graph variable, then setting it to null after the response is received and IsDone is true. It's a preliminary thing right now. I will have to test it and improve on it. I will probably have to handle errors as well.
     
  4. apiotuch_unity

    apiotuch_unity

    Joined:
    Jun 28, 2019
    Posts:
    138
    Also, I could have made sure the parent graph's Button Event was set to coroutine in the Visual Script inspector. That would have stopped the Wait node from erroring out.