hendryanw
hendryanw

Reputation: 1937

How cURL POST image file works?

I am trying to use the new Facebook Messenger Platform API to send image message using image file from application directory.

Facebook gives the example using cURL like below:

curl  \
  -F recipient='{"id":"USER_ID"}' \
  -F message='{"attachment":{"type":"image", "payload":{}}}' \
  -F filedata=@/tmp/testpng.png \
  "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"

But I am trying to use the API with C#. For your information, I have successfully use the API if I use the file from an internet url.

I have tried to fill in the filedata property by using base64 string of the image file, but unsuccessful.

Kindly explain how does cURL works with the given file path especially image and create a POST request to the web server? And if possible, what options do I have to do it with C#?

Upvotes: 0

Views: 1198

Answers (1)

ManOVision
ManOVision

Reputation: 1893

The -F option is for form. This is equivalent to issuing a POST request with the Content-Type header of multipart/formdata and the request body containing all the key-value pairs listed with a proper boundary set. cURL will read the binary data and put the bytes in the correct boundary in the request. There are many examples online for C# to submit a multipart/formdata request. Look into HttpClient or WebClient file uploads and you'll find what you need.

I'll be away from a computer for a few days and submitting sample code from a mobile device isn't the easiest thing to do. If you need some sample code, let me know.

Upvotes: 1

Related Questions