Solved

Getting 400 "Bad Request" error while uploading file to s3 during bulk mutation operation

swastik
Shopify Partner
3 1 1

While uploading jsonl file to Shopify s3 I'm getting bad request error. 

Function used for uploading jsonl file to s3

private void uploadStagingFileToShopifyS3(JsonNode jsonResponse, String outputFileName) {
  Map<String, String> parameters = new HashMap<>();
  log.info("uploadStagingFileToShopifyS3 for filename:{}", outputFileName);
  try {
    File file = new File(outputFileName);
    JSONArray jsonArray =
            JsonPath.read(jsonResponse.toString(), "$.responseObject.data.stagedUploadsCreate.stagedTargets[0].parameters");
    for (int index = 0; index < jsonArray.size(); index++) {
      Map<String, String> paramMap = (Map<String, String>) jsonArray.get(index);
      parameters.put(paramMap.get("name"), paramMap.get("value"));
    }
    String url = "https://shopify.s3.amazonaws.com";
    RequestBody body = new MultipartBody.Builder().addFormDataPart("key", parameters.get("key"))
            .addFormDataPart("x-amz-credential", parameters.get("x-amz-credential"))
            .addFormDataPart("x-amz-algorithm", parameters.get("x-amz-algorithm"))
            .addFormDataPart("x-amz-date", parameters.get("x-amz-date"))
            .addFormDataPart("x-amz-signature", parameters.get("x-amz-signature"))
            .addFormDataPart("policy", parameters.get("policy"))
            .addFormDataPart("acl", parameters.get("acl"))
            .addFormDataPart("Content-Type", parameters.get("Content-Type"))
            .addFormDataPart("success_action_status", parameters.get("success_action_status"))
            .addFormDataPart("file", file.getName(),
                    RequestBody.create(MediaType.parse("text/jsonl"), file))
            .setType(MultipartBody.FORM).build();

    Request request = new Request.Builder().post(body).url(url).build();
    OkHttpClient client = new OkHttpClient();
    Response response = client.newCall(request).execute();
    if (response.code() == 201) { // getting 400 Bad Request here
      System.out.println("Success");
    }
  } catch (Exception e) {
    log.error("Error while uploading staging file to shopify s3", e);
  }
}

  

Accepted Solution (1)
swastik
Shopify Partner
3 1 1

This is an accepted solution.

This issue is resolved.

Due to some reason my file operation was failing and My program was trying to upload empty file(File with no contents). That's why I was getting Bad Request error

 

Thank you for considering my question.

View solution in original post

Replies 2 (2)

awwdam
Shopify Staff
249 42 36

Hey @swastik,

To confirm here, you aren't getting an errors when making the stagedUploadsCreate request to using the Admin API, but rather for the proceeding POST request to https://shopify.s3.amazonaws.com to upload the staging file? 

Just to be sure, I successfully executed a few requests manually using cURL and our docs, but those didn't run into errors in my test environment.

I am not familiar with your shared implementation, but wanted to ask a few questions:

1. Can you confirm if the app is outputting raw data that mirrors how the multipart form is structured in our example docs? 

2. Do you get these same error when testing through the process manually, for example using a desktop http or API client? 

If yes to both, and you are able to replicate this behavior, please utilize verbose logging through your http client and pass on any details pertaining to time frame, frequency, etc. From there I can take a closer look, and pass on best next steps or insights moving forward.

I will keep an eye out here - Cheers!

awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

swastik
Shopify Partner
3 1 1

This is an accepted solution.

This issue is resolved.

Due to some reason my file operation was failing and My program was trying to upload empty file(File with no contents). That's why I was getting Bad Request error

 

Thank you for considering my question.