400 Response REST API Creating Blog when using PostImageFileUploadContext

IF we remove the PostImageFileUploadContext parameter it posts fine, however the doucmetnation states this accepts a string. What are we doing wrong?

Parents Reply Children
  • Then I would make sure you are actually uploading the image correctly

  • How can we confirm that?

    This is the data we are getting from the upload repost:

    {{
      "UploadedFile": {
        "UploadContextId": "5bb07d0a-dbc9-489b-97fc-f08609d446cc",
        "DownloadUrl": "http://localhost/cfs-file/__key/CommunityServer-Components-MultipleUploadFileManager/5bb07d0a_2D00_dbc9_2D00_489b_2D00_97fc_2D00_f08609d446cc-2100-complete/Gruppenfoto_5F00_3_5F00_komprimiert_2D00_1.jpg",
        "TotalChunks": 1,
        "CurrentChunk": 0,
        "FileName": "Gruppenfoto_3_komprimiert-1.jpg"
      },
      "Errors": null
    }}

    This is the code we are using to upload the file:

      // Create request
                            var request = (HttpWebRequest)WebRequest.Create(url + "/api.ashx/v2/cfs/temporary.json");
                            request.Headers.Add("Rest-User-Token", restUserToken);
                            request.ContentType = "multipart/form-data; boundary=" + boundary;
    
                            // Collect necessary request information
                            var formData = new Dictionary<string, string>
                        {
                            {"UploadContextId", uploadContextId.ToString()},
                            {"FileName", fileName},
                            {"CurrentChunk", currentChunk.ToString()},
                            {"TotalChunks", totalChunks.ToString()},
                        };
    
                            // Add data to the multi-part form
                            var requestData = new StringBuilder();
                            foreach (var item in formData)
                            {
                                requestData.Append($"--{boundary}\r\nContent-Disposition: form-data; name=\"{item.Key}\"\r\n\r\n{item.Value}\r\n");
                            }
    
                            // Add the file itself
                            requestData.Append($"--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{fileName}\"\r\nContent-Type: {fileContentType}\r\n\r\n");
    
                            // Prepare data chunk
                            var startDataBytes = Encoding.UTF8.GetBytes(requestData.ToString());
                            var chunk = rdr.ReadBytes(MAX_CHUNK_SIZE_BYTES);
                            var endDataBytes = Encoding.UTF8.GetBytes($"\r\n--{boundary}--\r\n");
    
                            // Combine all the request data into one array
                            var bytesToSend = new byte[startDataBytes.Length + chunk.Length + endDataBytes.Length];
                            startDataBytes.CopyTo(bytesToSend, 0);
                            chunk.CopyTo(bytesToSend, startDataBytes.Length);
                            endDataBytes.CopyTo(bytesToSend, startDataBytes.Length + chunk.Length);
    
                            try
                            {
                                // Make request and read results
                                var response = SendPostRequest(request, bytesToSend);
                                if (response?["Errors"] != null)
                                {
                                    return string.Join("\r\n", response["Errors"]);
                                }
                                return response;
                            }
                            catch (Exception ex)
                            {
                                return InvestigateError(ex);
                            }

  • We are seeing this error in our exceptions from trying to load the image: 

  • Did you go and verify the file integrity in the filestorage to make sure you uploaded correctly?