Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Using Unity for subscription lists

Discussion in 'Scripting' started by slaga, Mar 7, 2021.

  1. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    Has anyone managed to do that besides mailchimp? i have found a script that works for it : https://github.com/fiftytwo/MailChimpSubscriber

    but im trying to use klaviyo and i just cannot make it work. i have read through their api for post requests but had no luck in either using the above script or using the one i have for the leaderboard, which is this ( dont work)


    Code (CSharp):
    1. private IEnumerator SendData()
    2.     {
    3.  
    4.         WWWForm form = new WWWForm();
    5.         form.AddField("email", emailField.text);
    6.         form.AddField("api_key", "my api key goes here");
    7.         UnityWebRequest www = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/lists?api_key=my api key", form);
    8. // i have also tried it like this :
    9. //UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/{LIST_ID}/subscribe
    10.  
    11.         yield return www.SendWebRequest();
    12.  
    13.         if (www.isNetworkError || www.isHttpError)
    14.         {
    15.             Debug.Log(www.error);
    16.         }
    17.         else
    18.         {
    19.             Debug.Log("Registered");
    20.         }
    21.         Debug.Log(emailField.text);
    22.     }
    i either get a 404 or a 400 error code. now if i use the mailchimp script i get an error with the api key format
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,008
    As with all network code, get it working with Postman or curl first. That makes it NOT a Unity problem. Solve the networking first. You must solve that or anything else is irrelevant.

    Once you have that working, then try and port it to UnityWebRequest. If it still doesn't work, one handy debug solution is to hook up a proxy like Charles and compare the functioning output from Postman / curl to the output from Unity, and from that reason about where your issues are.
     
    Last edited: Oct 5, 2023
    siddharth3322 likes this.
  3. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    thanks for the reply, i managed to post a request through postman and now am looking for a way to put a json string into unity so i can link it and try it out! Basiclay on postman this is the url that posts the request

    Code (CSharp):
    1. https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]
    but it also needs a json text

    Code (CSharp):
    1.  {
    2. "profiles" :[
    3.         {
    4.             "email": "george.washington@example.com"
    5.         }
    6.  
    7.     ]
    8. }
    which im now trying to find out how to make the value be the unity string data i need
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,008
    I'm guessing you MIME encode the JSON and add it as a field to the form?
     
  5. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    well im looking into it! found some threads here doing that but yeah i still need to fully understand it and try it out! unless you have a general idea and would be willing to help once more!!
     
  6. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    ive gotten this far

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Text;
    6. using UnityEngine;
    7. using UnityEngine.Networking;
    8. using UnityEngine.UI;
    9.  
    10. public class MailingScript : MonoBehaviour
    11. {
    12.     public InputField email;
    13.  
    14.     [Serializable]
    15.     public class Email
    16.     {
    17.         public string email;
    18.     }
    19.     public IEnumerator Login(string bodyJsonString)
    20.     {
    21.         Email playerInstance = new Email();
    22.         playerInstance.email = email.text;
    23.         string playerToJson = JsonUtility.ToJson(playerInstance);
    24.         Debug.Log(playerToJson);
    25.         Debug.Log("String"+ bodyJsonString);
    26.         email.text = bodyJsonString;
    27.         UnityWebRequest req = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]", bodyJsonString);
    28.         req.SetRequestHeader("content-type", "application/json");
    29.         yield return req.SendWebRequest();
    30.         if (req.isNetworkError || req.isHttpError)
    31.         {
    32.             Debug.Log(req.error);
    33.         }
    34.         else
    35.         {
    36.             Debug.Log("Form upload complete!");
    37.         }
    38.  
    39.     }
    40.  
    41.     public void Upload()
    42.     {
    43.         StartCoroutine(Login(email.text));
    44.     }
    45.  
    46.  
    47. }
    48.  
    which debugs the text email but i get an error 400 bad request. and i think its due to the json not being as i initialy posted?
     
    Last edited: Mar 7, 2021
  7. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    im going to try once more in case someone can help! i got to a point i can make my string to json so i tried posting it but i still get the 400 error!

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Text;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7. using UnityEngine.UI;
    8.  
    9. public class MailingScript : MonoBehaviour
    10. {
    11.     public InputField emailText;
    12.     [Serializable]
    13.     public class Profile
    14.     {
    15.         public string email;
    16.     }
    17.     [Serializable]
    18.     public class MyClass
    19.     {
    20.         public Profile[] profiles;
    21.     }
    22.    
    23.     IEnumerator SaveData()
    24.     {
    25.         MyClass myClass = new MyClass();
    26.         myClass.profiles = new Profile[1];
    27.         myClass.profiles[0] = new Profile();
    28.         myClass.profiles[0].email = emailText.text;
    29.         string json = JsonUtility.ToJson(myClass.profiles[0]);
    30.         UnityWebRequest www = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]", json);
    31.         www.SetRequestHeader("content-type", "application/json");
    32.         yield return www.SendWebRequest();
    33.  
    34.         if (www.isNetworkError || www.isHttpError)
    35.         {
    36.             Debug.Log(www.error);
    37.         }
    38.         else
    39.         {
    40.             Debug.Log("Form upload complete!");
    41.         }
    42.         Debug.Log(json);
    43.     }
    44.     public void Subscribe()
    45.     {
    46.         StartCoroutine(SaveData());
    47.     }
    48. }