REST Service binary json NETMF version?

Gentlemen, I have built this nice little server of mine that has opened a REST interface for some devices to report status and other stuff.

Works fine with simple json, but uploading an image requires some binary stuff, and I am wondering if that is possible.

The below curl command line works fine, any gurus around that can translate that into NETMF?


curl -v -X POST -S -H 'Accept: application/json' -u "user:login" -F "device=1111" \
                           -F "image=@ testimg.jpg;type=image/jpg"  \
                          127.0.0.1:8000/api/photo/

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:

hmm, that was not what I was aiming for :’(

I want to code some c# running on my Cobra ii that can upload data to this API.

but your code just needs to use HTTP with appropriate headers. @ James is just trying to help unravel what the format of these might need to be.

Sorry, I might have approached this question in the wrong way.

With NETMF I can submit data to Xively, Opensense, Thingspeak etc using JSON REST API calls. There is libraries and stuff for that.

But there is no libraries for uploading image to Flickr or Twitter, as far as my search has found.

My guess is that there is no option in the current NETMF core to do that, and thats why I am asking.

I have built my own server in Python that exposes a REST interface, which is capable of receiving images (tested with the curl in the org question).

Can anyone guide to some assistance with uploading images via HTTP. Thanks

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.

1 Like

Ok, promising, I will try to implement a test and let you know how it goes…

Thanks for directing me in the right direction… :clap:

Thank you for the encouraging words! I almost forgot, when you add the binary data it will need to be encoded in Base64 as with most transactions

Status:

Based on your direction I did some more digging.

Found this: WEBSITE.WS - Your Internet Address For Life™

Thanks to Hugon for that input.

And found this: https://www.ghielectronics.com/community/forum/topic?id=1010&page=2#msg29842

Where there is demonstrated multi-part message.

Getting ready to combine that. :think:

@ njbuch - Did you success to upload photo to flickr?