• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Arimac · Jan 25, 2019 at 02:58 AM · coroutinethreadshttpwebrequestrestrestful

Recursive Coroutine-starts with RestSharp stops at the first callback which starts another coroutine of the same

I have been using UnityWebRequest for REST calls through Unity but for a certain project I had to choose another option and started testing RestSharp for Unity (https://github.com/llamazoo/RestSharp-for-unity3d).

The application contains a lot of API calls, therefore I have made a single static class with a static IEnumerator to send the requests and perform a callback to the monobehaviour class which started the coroutine.

There are some methods which Starts the API request coroutine, get the response and performs a callback which again Starts another coroutine of the same IEnumerator. All this works well with UnityWebRequests.

I have tried to do the same with RestSharp, adding a yield to the Async Execute of the API request. The first API call (let's say to endpoint-1) works well, receives the response and performs the callback with data from endpoint-1 as parameter. The callback method formats the data, and calls the same RestSharp IEnumarator with new data to endpoint-2. Here, the static method breaks out of the method as well as the Coroutine. (Which allows only single API call with a response per moment)

The following is my static class

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using RestSharp;
 using System.Net;
 
 public static class RestSharpUtil
 {
     public static IEnumerator request_routine_POST(string url,string json, System.Action<string> callback, System.Action<string> failCallback=null){
         Debug.Log("Request :  "+json);
 
 
         RestClient w = new RestClient(url);
         RestRequest request = new RestRequest(Method.POST);
 
 
 
         request.AddHeader("Content-Type", "application/json");
         request.AddHeader("Accept", "application/json");
 
         request.Parameters.Clear();
 
         request.AddParameter("application/json", json, ParameterType.RequestBody);
 
         yield return w.ExecuteAsync(request,(response,AsyncHandler)=> {
 
             if(response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.Accepted){
                 Debug.Log("Success!-> " + response.Content);
 
                 callback(response.Content);
                 Debug.Log("After Request");
 
             }else{
 
                 LoadingPanel.ShowError("Something went wrong, Try again!");
 
 
             }
         });
 
         Debug.Log("Action Complete");
     }
 }


And the following are my methods (main one and the callback)

 public void test(){
 
        string customerdata= "json1";
 
        StartCoroutine(RestSharpUtil.request_routine_POST(endpoint-1, customerdata, OnResponse));
 
  }
 
 void OnResponse(string res){
 
         string otherdata = "some processed data with res";
 
         StartCoroutine(RestSharpUtil.request_routine_POST(endpoint-2, otherdata, OnResponse2));
 
 }


OnResponse2 is just a blank method to show the content. However, performing the Coroutine in OnResponse prints the earlier request, not the new one.

My Result is,

 Request : json1
 Action Complete
 Success -> response
 Success -> response


It never prints the "Action Complete" or "After Request". What am I doing wrong here?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get into the data returned from a UnityWebRequest ? 2 Answers

Coroutine Death on Scene Change 3 Answers

Set Content-Length header for UnityWebRequest POST requests in 2017.3? 1 Answer

How to handle a slow function from a dll 1 Answer

Return value from coroutine to non monobehaviour 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges