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
  • Are you evaluating the actual responses to get the error?  Also again, please do not use the SDK.  You cannot use it for file uploading which this requires as the post image url  context is the string used when uploading the actual image.  Second, you provided the string, but no PostImageFileName.

    I have stated this in previous threads as well, you do not concatenate data into a url.  Blog Id is a Path Parameter, you set the url to /blogs/{blogid}/posts.xml  then define blogid as a path parameter

  • I am uploading the images outside of the SDK. I am just using it here for ease of use.

    I tired using {blogid} instead of the int value and it was throwing errors so i went back to passing in the INT.

    Implementing your suggestions I am still getting the same errors:

  • Did you actually create and set blogid  is PathParameters collection?  NOT PostParameters

  • Ah ha, progress. Now the post is posting and the Image URL but the image is returning a 400 error.


    Request:

     

  • 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);
                            }

Reply
  • 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);
                            }

Children
No Data