Title: com.HTTPRequest.doFormEncodedRequest and binary data Post by: Rodrigo V. on August 19, 2014, 11:55:58 pm Hello,
can we use the method com.HTTPRequest.doFormEncodedRequest to send binary data (jpg) using POST method like CURL? example in CURL: 'curl -v -X POST -H "multipart/form-data" --form "image=@/tmp/2.jpg" "http://PSKEY:@psdomain.com/api/images/products/984"' (note the @ meaning binary content for the file /tmp/2.jpg) we are connecting with PrestaShop WebService succesfully using com.HTTPRequest + XML (products, customers,..etc) but no way to upload binary images (it works perfect with CURL command). We try a lot of combinations (HTTPPart class, Diferent headers... and so on) but no success. żany idea? Thanks! Title: Re: com.HTTPRequest.doFormEncodedRequest and binary data Post by: Frank G. on August 20, 2014, 10:28:24 am Hi,
To send a file in a multipart request, you have to use com.HTTPPart.CreateAttachment() if it is not the main part, and use one of the doXXXRequest() methods for the main part. But in your case, you have only one part, so it must be handled as the main part via the doDataRequest() method, and notify the API to perform multipart anyway with the com.HTTPRequest.setMultipart() method. This gives something like following : IMPORT COM CONSTANT SERVER_URL = "http://host:port/XXX" CONSTANT FILE_NAME = "my_filename.jpg" MAIN DEFINE req com.HTTPRequest DEFINE resp com.HTTPResponse DEFINE blob BYTE # Create Request LET req = com.HTTPRequest.Create(SERVER_URL) CALL req.setMethod("POST") # Switch to multipart CALL req.setMultipartType("mixed",NULL,NULL) # Load main part as a blob LOCATE blob IN MEMORY CALL blob.readFile(FILE_NAME) # Set right content-type CALL req.setHeader("Content-Type","image/jpg") # Perform request CALL req.doDataRequest(blob) # Process response LET resp = req.getResponse() DISPLAY "HTTPStatus : ",resp.getStatusCode() END MAIN Does this help ? Frank Title: Re: com.HTTPRequest.doFormEncodedRequest and binary data Post by: Rodrigo V. on August 20, 2014, 10:59:53 am Hello Frank,
I tried this before posting without success, the problem is that the server was expecting the string 'image=' before the binary content and I donĄt find any way to do that. Title: Re: com.HTTPRequest.doFormEncodedRequest and binary data Post by: Frank G. on August 20, 2014, 12:09:24 pm I registered bug #GWS-438 : Sending only one file in a multipart request doesn't set the Content-Disposition header.
You can maybe try to force that header as following : CALL req.setHeader("Content-Disposition","form-data; name=\"image\"") But I'm not sure the server understand that. Frank P.S: In my previous sample I wrote mixed as multipart type but it is form-data Title: Re: com.HTTPRequest.doFormEncodedRequest and binary data Post by: Rodrigo V. on August 20, 2014, 12:41:00 pm I did it yesterday and it doesn't work.
|