0
\$\begingroup\$

hey everyone any ideas why im getting a 400 error? im trying to post a json to a subscription list. doing that through postman works just fine!

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

public class testing : MonoBehaviour
{
    //public InputField field;
    [SerializeField]
    private Email _email = new Email();
    private string URL = "https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]";
    IEnumerator SaveIntoJson()
    {
        string data = JsonUtility.ToJson(_email);
        System.IO.File.WriteAllText(Application.persistentDataPath + "Data.json", data);

        //UnityWebRequest www = UnityWebRequest.Post("URL", data);
        //www.SetRequestHeader("Content-type", "application/json");
        UnityWebRequest request = UnityWebRequest.Put(URL, data);
        request.SetRequestHeader("Content-Type", "application/json");
        yield return request.SendWebRequest();
        //yield return www.SendWebRequest();

        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
        Debug.Log(data);
    }


    public void SaveData()
    {
        StartCoroutine(SaveIntoJson());
    }
}
[System.Serializable]
public class Email
{
    public List<Profiles> profiles = new List<Profiles>();
}

[System.Serializable]
public class Profiles
{
    public string email; 
}
\$\endgroup\$
2
  • \$\begingroup\$ Is data what you expect it to be? \$\endgroup\$ – Tyyppi_77 Mar 8 at 10:55
  • \$\begingroup\$ yeah i used debug log on request,handlertext and found out that No JSON object could be decoded . so i decoded it and now works! \$\endgroup\$ – stratos la Mar 8 at 11:02
0
\$\begingroup\$
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class testing : MonoBehaviour
{
    //public InputField field;
    [SerializeField]
    private Email _email = new Email();
    private string URL = "urlHere&profiles=[]";

    public void SaveData()
    {
        string data = JsonUtility.ToJson(_email);
        System.IO.File.WriteAllText(Application.persistentDataPath + "Data.json", data);
        StartCoroutine(SaveIntoJson(URL , data));
    }
    IEnumerator SaveIntoJson(string url, string data)
    {
        var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
        request.SetRequestHeader("Content-Type", "application/json");
        var jsonBytes = Encoding.UTF8.GetBytes(data);
        request.uploadHandler = new UploadHandlerRaw(jsonBytes);

        request.downloadHandler = new DownloadHandlerBuffer();

        yield return request.SendWebRequest();

        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
            Debug.Log(request.downloadHandler.text);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
        Debug.Log(data);
    }

}
[System.Serializable]
public class Email
{
    public List<Profiles> profiles = new List<Profiles>();
}

[System.Serializable]
public class Profiles
{
    public string email; 
}
\$\endgroup\$
1
  • \$\begingroup\$ This answer would be better if it included some simple text explaining what you changed to fix the problem, and how that works. \$\endgroup\$ – DMGregory Mar 8 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.