Search Unity

Question Troubleshoot HttpException`1: HTTP/1.1 400 Bad Request

Discussion in 'Cloud Save' started by OUTERDARKNESS, Aug 29, 2022.

  1. OUTERDARKNESS

    OUTERDARKNESS

    Joined:
    Feb 27, 2013
    Posts:
    35
    Hello, when using Cloud Save, I get an error HttpException`1: HTTP/1.1 400 Bad Request.
    The data I am sending is a string created by serialization of an object containing primitive datatypes (int, float, bool, string). The JSON serialization appears valid (attached), I don't see any red flags looking at the data.

    What is the best way to troubleshoot this? Are there certain characters that are invalid that I need to look out for?

    Unity Editor ver: 2021.3.3f1 / Windows
    Cloud Save ver: 2.0.1

    Code (CSharp):
    1. string payload = JsonUtility.ToJson(SceneState);
    2. var dataCollection = new Dictionary<string, object> { { key, payload } };
    3. await CloudSaveService.Instance.Data.ForceSaveAsync(dataCollection);

    Error Log:
    CloudSaveValidationException: There was a validation error. Check 'Details' for more information.
    HttpException`1: HTTP/1.1 400 Bad Request
    Unity.Services.CloudSave.Internal.Http.ResponseHandler.HandleAsyncResponse (Unity.Services.CloudSave.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at Library/PackageCache/com.unity.services.cloudsave@2.0.1/Runtime/Generated/Runtime/Http/ResponseHandler.cs:103)
    Unity.Services.CloudSave.Internal.Http.ResponseHandler.HandleAsyncResponse[T] (Unity.Services.CloudSave.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at Library/PackageCache/com.unity.services.cloudsave@2.0.1/Runtime/Generated/Runtime/Http/ResponseHandler.cs:186)
    Unity.Services.CloudSave.Internal.Apis.Data.DataApiClient.SetItemBatchAsync (Unity.Services.CloudSave.Internal.Data.SetItemBatchRequest request, Unity.Services.CloudSave.Internal.Configuration operationConfiguration) (at Library/PackageCache/com.unity.services.cloudsave@2.0.1/Runtime/Generated/Runtime/Apis/DataApi.cs:249)
    Unity.Services.CloudSave.ApiClient.ForceSaveAsync (System.Collections.Generic.Dictionary`2[TKey,TValue] data) (at Library/PackageCache/com.unity.services.cloudsave@2.0.1/Runtime/ApiClient.cs:57)
    Unity.Services.CloudSave.SaveDataInternal.ForceSaveAsync (System.Collections.Generic.Dictionary`2[TKey,TValue] data) (at Library/PackageCache/com.unity.services.cloudsave@2.0.1/Runtime/SaveData.cs:158)
    Rethrow as CloudSaveValidationException: There was a validation error. Check 'Details' for more information.
    Unity.Services.CloudSave.SaveDataInternal.ForceSaveAsync (System.Collections.Generic.Dictionary`2[TKey,TValue] data) (at Library/PackageCache/com.unity.services.cloudsave@2.0.1/Runtime/SaveData.cs:167)
    CloudData.SaveData (System.String key, System.Object data, CloudData+OnReturn_Empty callback) (at Assets/01_Scripts/User/CloudData.cs:59)
    System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <6073cf49ed704e958b8a66d540dea948>:0)
    UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
    UnityEngine.UnitySynchronizationContext.Exec () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
    UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <f1212ad1dec44ce7b7147976b91869c3>:0)
     

    Attached Files:

  2. MileyUnity

    MileyUnity

    Joined:
    Jan 3, 2020
    Posts:
    92
    Hi there,

    Whilst the error isn't all that clear, I think it means that your data is too big. The CloudSave SDK serializes the incoming objects to json for you and given that you are sending in a string rather than an object it will have to re-serialize it in order to make it transmittable via json. This means that your 14.6Kb json string may end up being over the 16Kb limit per slot and thus causing the 400 bad request.

    It would be best to avoid using the JsonUtility to serialize your outgoing data and instead put in the object itself and let CloudSave take care of the serialization. I would also recommend to look into a different way of storing your data as you may easily go over the 16Kb limit per slot and would again get a 400 bad request response.
     
    SebT_Unity likes this.
  3. OUTERDARKNESS

    OUTERDARKNESS

    Joined:
    Feb 27, 2013
    Posts:
    35
    Bingo! You are correct, the serialization of the string was adding escape characters to all quotes, inflating the files size past the limit. I was able to split up the data into smaller chunks to meet the requirement. Thanks for your help.

    BTW, I've been using JsonUtility because the Newtonsoft serializer used by Cloud Save breaks when trying to serialize a Vector3.

    JsonSerializationException: Self referencing loop detected for property 'normalized' with type 'UnityEngine.Vector3'
     
  4. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    I had the exact same error however it seems you can't fetch keys from the Unity Service that don't exist. So I asked for a key that doesn't exist and I got a bad error code.

    Shouldn't Unity just send me no information back with that key or not include it in the resulting dictionary? Is this intentional? Do I need to validate every key I send up to the Cloud?
     
  5. Catoag83

    Catoag83

    Joined:
    Apr 30, 2014
    Posts:
    5
    I also had the bad request error and tried shortening the string. but in my case it was the key. it cant have spaces it seems. kinda obvious, but thoght i share in case anyone else stumbled into it.
     
    MaxBackcountry and OUTERDARKNESS like this.
  6. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    why in the feck is this not clearly documented, spaces are perfectly valid JSON, ffs
    ty Catoag

    EDIT: here is the docs RE the allowed characters for keys https://services.docs.unity.com/cloud-save/v1/#tag/Data/operation/setItem
     
    Last edited: Jul 30, 2023
  7. MaxBackcountry

    MaxBackcountry

    Joined:
    Jan 24, 2014
    Posts:
    13
    Hey Catoag83, I actually stumbled into it and you saved me some headache :) Thanks a lot!
     
  8. Ziplaw

    Ziplaw

    Joined:
    Sep 24, 2018
    Posts:
    4
    This is not talked about enough, this is still a problem with Cloud Save in 2022.3.2, where this is supposedly production ready, and we still don't have a way to change the config of Newtonsoft's ReferenceLoopHandling