1

I'm currently integrating Walmart into our application and I'm trying to post an Item Feed through the API method. I've already succeeded in posting a feed from the ARC tool, so I already know how to model the post request for it, my only problem is that I'm having difficulties in using it in my code, while using the Resttemplate.

Below is the request (from ARC - Chrome) that succeeded to post my item (I can see them in my Walmart Seller console) and the Java code for it (trimmed the credentials for security reason):

Obs: The signature and the other fields used in the Java code for the requests are correctly generated, because I used them in my request from ARC as well, so there's no issue in the headers' values, but in how I'm trying to set the file in the body request, as far as I can tell:

enter image description here

And the code for generating the headers and sending the post request with Resttemplate: ..............................

        headers = new HttpHeaders();
        headers.set("WM_SVC.NAME", "Walmart Marketplace");
        headers.set("WM_QOS.CORRELATION_ID", "123456abcdef");
        headers.set("WM_SEC.TIMESTAMP", timestamp);
        headers.set("WM_SEC.AUTH_SIGNATURE", signatureString);
        headers.set("WM_CONSUMER.ID", "my-consumer-id-cut-for-security-reasons");
        headers.set("WM_CONSUMER.CHANNEL.TYPE", "my-channel-type");
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
        headers.set("Host", "https://marketplace.walmartapis.com");

..............................

    File file = new File("C:\\walmartfeed.xml");
    DiskFileItem fileItem = new DiskFileItem("file", "text/xml", false, file.getName(), (int) file.length(), file.getParentFile());
    fileItem.getOutputStream();
    MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

    MultiValueMap<String, Object> parts =
            new LinkedMultiValueMap<String, Object>();
    parts.add("file", new ByteArrayResource(multipartFile.getBytes()));

    HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(parts, headers);
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    restTemplate.getMessageConverters().add(new MultipartAwareFormHttpMessageConverter());

    ResponseEntity<FeedAcknowledgement> response = restTemplate.exchange("https://marketplace.walmartapis.com/v3/feeds?feedType=item", org.springframework.http.HttpMethod.POST, request, FeedAcknowledgement.class);

I am receiving a 400 status code saying it's a bad request. Did you manage to post feeds for Walmart or is there any other way to use Resttemplate to post multipart/form-data / files ?

Thanks

andreealib
  • 63
  • 10
  • `400 error` means that your request is a bad request. So either the headers or query parameter is incorrect. Please provide details of the API you are trying to call. – Akash Aug 04 '17 at 10:58
  • This is the Walmart request that I'm trying to do: https://developer.walmart.com/#/apicenter/marketPlace/latest#bulkCreateUpdateItems As I said in the post, I know how the request looks in ARC/Postman, I already succeeded in calling it from an utilitary tool, my problem is how to I transform and use the request from the first picture from above in my code, using Resttemplate? Bottom line, how do I properly use Resttemplate to post a file (with Content-type: multipart/form-data), how do I properly set the file in the request body? – andreealib Aug 04 '17 at 11:04
  • Can you try not sending `parts` in the `request` and see the result to get some clue? – Akash Aug 04 '17 at 11:22
  • Yes, I still got a 400 bad Request. Problem is I cannot attach the xml file to the body request properly with Resttemplate with Content-Type of mutipart/form-data. Do I need to put any others message converters or use other types of inputstream classes? – andreealib Aug 04 '17 at 11:59

0 Answers0