Using Unity for subscription lists

Has anyone managed to do that besides mailchimp? i have found a script that works for it : GitHub - fiftytwo/MailChimpSubscriber: Subscribe users to your MailChimp list right from your Unity game.

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)

private IEnumerator SendData()
    {

        WWWForm form = new WWWForm();
        form.AddField("email", emailField.text);
        form.AddField("api_key", "my api key goes here");
        UnityWebRequest www = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/lists?api_key=my api key", form);
// i have also tried it like this : 
//UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/{LIST_ID}/subscribe

        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Registered");
        }
        Debug.Log(emailField.text);
    }

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

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.

1 Like
UnityWebRequest error connection API
WebRequest Proxy Authentification
UnityWebRequest - Unable to complete SSL connection
How Can i Connect Unity to Postgres Database
Google Maps Integration for Real World Race Track
libcurl issue
A problem with WWW Form and PhP
SendWebRequest.Post() doesn't send variables
An error
Why Spawning is not working?
How to create a web session for one-time basic authentication
Error on http request
Unity WebRequest
User Image Upload
Problems with Unitywebrequest
webgl error on System.IO.StreamReader
How to use cURL in Unity
UnityWebRequest error Cannot connect to destination host
Can't send email to users
Calling method not working...
Working with excel spreadsheets?
UnityWebRequest not working on Android API level 33
Unable to load texture from server in iOS
Network Port - connecting Unity 3D with outside device
UnityWebRequest.Post not sending anything to my webserver.
Both UnityWebRequest and WWW are not able to download any file from LAN
Any sort of JSON deserializing stops code completely
Uploading File to Server from Android
ZeroMQ (ZMQ) deployed on Android?
Can no longer get response from HttpWebRequest when 400 returned at Unity 2020,2021
Simple Mobile database connection
C# to PHP - WWW Update?
UnityWebRequestException:Unknown Error  WebGL
Trying to get float to MySQL, it gets rounded.
Message from Nodejs websocket to Unity
AWS / Cognito - difficulties with login
UnityWebRequest.Post dosen't work
HttpClient - Timeout - Different behavior between VS and Unity
RSA server - client simulation exchange keys
UnityWebRequest to Local Network Shared Folder
POST request doesn't work (UnityWebRequest)
How to change the value of ToggleGroup to a string? And how to pass Togglegroup values to WWWFrom?
Troubleshooting PHP Login Script
GPT Api Integration
convert webRequest.downloadHandler.text to an array
Steps to send and recv image
Can post data using toggle question to Google form?
Unity Texture 2D from Uint64List byte[]
yield return web.SendWebRequest() crashes on Samsung Galaxy Tab A7 Lite
Communication between app and service
is a non-blocking HttpWebRequest possible?
Unity Player authentication and Request with JSON
Network debugger like in googleChrome devtool for HTTP requests debugging
How can I remove an array item?
Assetbundle DownloadHandlerFile
IM back DECODING networking IMAGE BYTES
Assistance with JSON response
Can't retrieve DownloadHandler.text from a UnityWebRequest.Put that has failed
mqtt Certificate fails.
Send email using PHP
UnityWebRequest shows an "?" when downloading an image from a server
Which premissions do I need for MySQL connection in Android
issue with nonascii filename when using synology nas api.
Can I send information to a browser from a Unity app built for Windows?
Post request accessing the image.
Rest API post: Issues with new server
Azure OCR on local image
Unity and ZeroMQ?
Android file upload working in Simulator but not phone¿¿¿¿
Uploading Images Android
An issue with Open AI API and nullable float variable(float?)
TCP Connection Refused
UnityWebRequestTexture.GetTexture() web request is silently failing
Use Get instead Post
using stable diffusion
Dataverse Web API authentication
Suggested way of receiving audio data through TCP conection and update audio clip in Unity
Coroutine to make a unitywebrequest won't execute
HttpClient Fails to Get Access
[Solved] Best Way to Upload and Store CSV Data from a Unity WebGL Game
Getting time from a server؟؟؟
Post to php issue!
The problem is in getting the time from the server
Recieving video continuous grey image
how to use UnityWebRequest on visual scripting ?
How can I debug WebSockets requests?
Float Array To AudioClip
Trying to get elevenlabs TTS API working
Package manager 401 error with custom scoped registry
Can Unity WebGL use the Amazon AWS Signing V4 without SDK? Like js?
Unable to send PUT request via UnityWebRequest
Dll import error
UnityWebRequest and image upload

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

https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]

but it also needs a json text

 {
"profiles" :[
        {
            "email": "george.washington@example.com"
        }

    ]
}

which im now trying to find out how to make the value be the unity string data i need

I’m guessing you MIME encode the JSON and add it as a field to the form?

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!!

ive gotten this far

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class MailingScript : MonoBehaviour
{
    public InputField email;

    [Serializable]
    public class Email
    {
        public string email;
    }
    public IEnumerator Login(string bodyJsonString)
    {
        Email playerInstance = new Email();
        playerInstance.email = email.text;
        string playerToJson = JsonUtility.ToJson(playerInstance);
        Debug.Log(playerToJson);
        Debug.Log("String"+ bodyJsonString);
        email.text = bodyJsonString;
        UnityWebRequest req = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]", bodyJsonString);
        req.SetRequestHeader("content-type", "application/json");
        yield return req.SendWebRequest();
        if (req.isNetworkError || req.isHttpError)
        {
            Debug.Log(req.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }

    }

    public void Upload()
    {
        StartCoroutine(Login(email.text));
    }


}

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?

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!

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class MailingScript : MonoBehaviour
{
    public InputField emailText;
    [Serializable]
    public class Profile
    {
        public string email;
    }
    [Serializable]
    public class MyClass
    {
        public Profile[] profiles;
    }
   
    IEnumerator SaveData()
    {
        MyClass myClass = new MyClass();
        myClass.profiles = new Profile[1];
        myClass.profiles[0] = new Profile();
        myClass.profiles[0].email = emailText.text;
        string json = JsonUtility.ToJson(myClass.profiles[0]);
        UnityWebRequest www = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]", json);
        www.SetRequestHeader("content-type", "application/json");
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
        Debug.Log(json);
    }
    public void Subscribe()
    {
        StartCoroutine(SaveData());
    }
}