Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Msxmlhttp sending image data
Message
 
À
18/08/2014 16:48:02
Mike Yearwood
Toronto, Ontario, Canada
Information générale
Forum:
Visual FoxPro
Catégorie:
Contrôles ActiveX en VFP
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01603736
Message ID:
01606038
Vues:
83
Glad that works for you...

Come to think about it it is pretty annoying that natively Windows and even Web browsers don't have a native API to create POST data effectively. It's pretty lame that this has to be written. Even .NET didn't have a library until .NET 4.5 actually and that is also pretty sketchy.

An HTTP client that lets me easily post various kinds of encodings is one of the first things I create in any language I use that doesn't have it natively :-)

+++ Rick ---

>Hey Rick!
>
>You still rock! (even if the hair in your picture isn't as long as it was last we met).
>
>The sample userid and password does not actually upload the image. It just returns a dummy image. With the real userid and password, I got an image uploaded and the URL points to the correct image. I'm very much in favor of frameworks. I'm recommending we purchase.
>
>Thanks a bunch.
>
>Mike
>
>>Well, XmlHttp is a low level technology. It provides you with the basic send mechanism and it was never designed to directly provide you the protocol abstraction to post data in a specific format like multi-part forms or even URL encoded data. XmlHttp was created for working with XML that typically involves full as is HTTP buffers. It can be abstracted, but that's where the layer on top come in.
>>
>>That's what a tool like wwHttp provides - the abstraction for POSTing data properly etc.
>>It's easy to do with wwHttp and the following code works:
>>
>>
>>DO wwhttp
>>
>>loHttp = CREATEOBJECT("wwhttp")
>>loHttp.nHttpPostMode = 2  && Multipart form preset
>>
>>*** File key
>>loHttp.AddPostKey("file_path","c:\sailbig.jpg",.t.)
>>
>>*** Regular form variable key
>>loHttp.AddPostKey("format","json")
>>
>>loHttp.cUsername = "X0X0X0"
>>loHttp.cPassword = "1234a2345b3456c4567"
>>
>>lcJson = loHttp.HttpGet("https://api.realsatisfied.com/v1/image.json")
>>? lcJson
>>
>>*** Deserialize the Json
>>DO wwJsonSerializer
>>loSer = CREATEOBJECT("wwJsonSerializer")
>>loResult = loSer.DeserializeJson(lcJson)
>>? loResult
>>? loResult.image_public_url
>>
>>*** Preview the image in browser (note the image never updates - not here not with their sample page either)
>>GoUrl(loResult.image_public_url)
>>
>>
>>This code works and uploads the image and provides a public URL although regardless of whether you use the demo or the code it doesn't actually change the result picture.
>>
>>+++ Rick ---
>>
>>>Hi Rick
>>>
>>>They do have documentation. It's a very good sandbox/api.
>>>
>>>https://api.realsatisfied.com/api_docs.php#!/image/upload_image_file_POST
>>>
>>>I've tried Fiddler and while it's a cool technology, the output is making my eyes cross and my head swim. There is a lot going on under the covers to send data to a webservice.
>>>
>>>Your suggestion makes me wonder if it might have been better to just install PHP into the client's IIS and reproduce the sample PHP with TEXT...ENDTEXT rather than trying to translate the PHP to xmlhttp.
>>>
>>>
>>>
>>>
>>>>Sorry not familiar enough with PHP and its HTTP library to parse what they're doing. IThe first block is plain configuration of the HTTP client and then a file is sent in some sort of format. It looks like raw data to me, but couldn't say for sure.
>>>>
>>>>If I had to guess I'd say they are posting a raw JPG file to the server - it's not a file upload but a raw HTTP request:
>>>>
>>>>
>>>>Do wwHTTP
>>>>
>>>>loHTTP = CREATEOBJECT("wwhttp")
>>>>loHttp.cContentType = "img/jpeg"
>>>>loHttp.AddPostKey(FILETOSTR("C:\sailbig.jpg"))
>>>>lcResponse = loHttp.HttpGet("https://api.realsatisfied.com/v1/image.json")
>>>>
>>>>? lcResponse
>>>>
>>>>
>>>>Tried this but I get an error that says - Request JSON is invalid which seems to suggest the endpoint is expecting a JSON request. The PHP code isn't doing any of that though... I don't see any conversion to JSON from anything. Something is missing from that sample if you ask me.
>>>>
>>>>Something must be missing about that PHP code because nowhere does it set the content type or assign any other POST variables that seem to suggest the data would be in JSON.
>>>>
>>>>What I would do is see if you can run the PHP code then capture the HTTP trace with Fiddler. From that you can probably create the HTTP request you need to communicate with the service. Don't they have any documentation?
>>>>
>>>>+++ Rick ---
>>>>
>>>>
>>>>
>>>>>Hey Rick
>>>>>
>>>>>Can you translate this PHP curl code to vfp? I tried with libcurl, but I am not getting any success.
>>>>>
>>>>>$ch = curl_init("https://api.realsatisfied.com/v1/image.json");
>>>>>curl_setopt($ch, CURLOPT_HEADER, false);
>>>>>curl_setopt($ch, CURLOPT_USERPWD, "userid:password");
>>>>>curl_setopt($ch, CURLOPT_TIMEOUT, 30);
>>>>>curl_setopt($ch, CURLOPT_POST, true);
>>>>>$payload = array(
>>>>>"file_path"=>"@/env/david/workspace/realestatesat/public/images/td_agent_photo.jpg",
>>>>>);
>>>>>curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
>>>>>curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
>>>>>$json_return = curl_exec($ch);
>>>>>echo $json_return;
>>>>>exit();
>>>>>
>>>>>
>>>>>
>>>>>>If the data in the body is *just* raw binary data then the photo is not being sent by multipart-form. Multipart form includes mime headers for the data followed by the content and then separated by Mime boundaries.
>>>>>>
>>>>>>if they're sending raw data it's just a raw HTTP request with raw image content type (ie. Content-Type: image/jpeg).
>>>>>>
>>>>>>Just not sure how you can actually get the binary data into XmlHttp's send buffer. A COM string assignment in Send() will truncate at nulls (0 which might be present in binary data and images)...
>>>>>>
>>>>>>Verified though that binary results can now be read directly by VFP as they come back as VARTYPE() Q. Prior to the varbinary type this wasn't possible and you had to resort to ADO Streams to read the ResponseStream rather than ResponseBody.
>>>>>>
>>>>>>Either way - using XmlHttp is fraught with issues once you go beyond the basics - it's just very inconsistent in how data is accepted and read, not to mention the funky mostly undocumented attribute based customizations for extended functionality like authentication.
>>>>>>
>>>>>>+++ Rick ---
>>>>>>
>>>>>>>Hey Rick
>>>>>>>
>>>>>>>Thanks for contributing to this! I used fiddler2 to get the raw payload from the provider's own API testbed. I can see exactly what his end is expecting. Interestingly enough, the filetostr() vfp function of a jpg looks to me like the stuff in his payload.
>>>>>>>
>>>>>>>>Argh - save yourself the pain of using XmlHttp for anything but basic HTTP requests... seriously. So many issues with that and file uploads require separate Stream objects out of the ADO library.
>>>>>>>>
>>>>>>>>with wwHttp:
>>>>>>>>
>>>>>>>>
>>>>>>>>Do wwHTTP
>>>>>>>>
>>>>>>>>loHttp = CREATEOBJECT("wwHTTP")
>>>>>>>>loHttp.nHttpPostMode = 2  && multipart forms
>>>>>>>>loHttp.AddPostKey("image","c:\temp\sailbig.jpg",.T.)
>>>>>>>>loHttp.AddPostKey("notes","cool shot from the other day")
>>>>>>>>
>>>>>>>>lcResult = loHttp.HttpGet("http://somesite.com/uploadimages/")
>>>>>>>>IF (loHttp.nError != 0)
>>>>>>>>   ? loHttp.cErrorMsg
>>>>>>>>   RETURN
>>>>>>>>ENDIF
>>>>>>>>
>>>>>>>>? lcResult
>>>>>>>>
>>>>>>>>
>>>>>>>>more info here:
>>>>>>>>http://www.west-wind.com/webconnection/wwClient_docs/_0rs0twgr6.htm
>>>>>>>>
>>>>>>>>+++ Rick ---
>>>>>>>>
>>>>>>>>>Hi all
>>>>>>>>>
>>>>>>>>>I'm using msxmlhttp v 6.0 to get/post simple things. Now I've been asked to send an image. The enctype must be "multipart/form-data".
>>>>>>>>>
>>>>>>>>>o=CREATEOBJECT("Empty")
>>>>>>>>>ADDPROPERTY(m.o,"FirstName","Mike")
>>>>>>>>>ADDPROPERTY(m.o,"LastName","Yearwood")
>>>>>>>>>oJSON=CREATEOBJECT("Json")
>>>>>>>>>lcJSON = oJSON.Stringify(m.o)
>>>>>>>>>?m.lcJSON
>>>>>>>>>
>>>>>>>>>oHTTP = CREATEOBJECT("MSXML2.XMLHTTP.6.0")
>>>>>>>>>cusername="cantsay"
>>>>>>>>>cpassword="wontsay"
>>>>>>>>>cUrl="https://somewhere.com/overtherainbow"
>>>>>>>>>luResult = oHTTP.OPEN("GET", m.cURL, .F., m.cUserName, m.cPassword)
>>>>>>>>>lcEncode64=STRCONV(m.cUserName+":"+m.cPassword,13)
>>>>>>>>>oHTTP.SetRequestHeader("Authorization", "Basic "+m.lcEncode64)
>>>>>>>>>oHTTP.SetRequestHeader("Content-Type", "application/json")
>>>>>>>>>
>>>>>>>>>oHTTP.SEND()
>>>>>>>>>
>>>>>>>>>?oHTTP.Status()
>>>>>>>>>
>>>>>>>>>Where and how would I go about adding a filename and a data stream?
>>>>>>>>>
>>>>>>>>>Thanks
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform