I’m not an expert at the inner workings of curl as I have spent very little time with it. If your request is to translate the above command to a uri, here is how you would do it:
The -F switch adds information to the post field, and this is probably what is throwing off uploading the image. PHP and others will separate these fields from the POST fields by using what is called a boundary – you will most likely need to manually parse the entire POST to extract the image data. If you are uploading the image, you will need to make your own boundaries.
I am slightly confused by the "-F “image=@ testimg.jpg;type=image/jpg” " as the way the documentation is written would suggest that the testimg.jpg would be a resource on the server already.
-H replaces the HTTP header fields
-u adds a username/password to the transaction
The translated URI would be:
http://login@ 127.0.0.1:8000/api/photo/testimg.jpg for downloading http://login@ 127.0.0.1:8000/api/photo/ for uploading
For the uploading portion, this should help explain how to emulate the POST data properly with boundaries:
again, I’m coming from a position of little knowledge here too.
If I was going to investigate this, I’d use network monitor and sniff what CURL actually did. The HTTP POST data is key. You are probably in need of a multi-part POST message that includes the binary data.
REST services are dumb. They just deal with a stream of data (the POST message) and figure out how the content relates to things it can and can’t do. If it sees a section that it can act on, it will, so all you need is to properly construct the HTTP POST message and submit it.
@ njbuch - This is a rather detailed explanation of HTTP Multi-Part Form Data: Forms in HTML documents
However, you should be able to accomplish this easily either with Sockets or HttpWebRequest.
For HttpWebRequest, you will need to set the ContentType property to “multipart/form-data;” and add a boundary header field through the Headers member. You then must build your body. The body consists of, in this case, a single boundary section, with it’s own headers.
The options you would add with -F in the curl command would be added to [quote]Content-Disposition: form-data; name=“archivo”; filename=“… put filename here …”[/quote] separated by a semi-colon. The header section within the boundary must also be followed by CRLFCRLF as the main headers do.
Example gathered from google groups of the full HTTP transaction:
[quote]POST /vt/en/recepcion HTTP/1.1
Host: www.virustotal.com
Keep-Alive: 300
Connection: keep-alive
Content-Type: multipart/form-data;
boundary=---------------------------41184676334
Content-Length: <length of body below + length of file>
-----------------------------41184676334
Content-Disposition: form-data; name=“archivo”; filename=“… put filename here …”
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-Length: … put length of file data here …
… put binary file data here …
-----------------------------41184676334–[/quote]
You will most likely need to send the body of the message in multiple chunks, depending on image size.