0

I should start by saying I'm a novice in Curl/ backend/ server stuff. I have a local server running in an android app. While the device is connected to my machine via a cable I can POST to that server using Curl. I need to send a .vcf file alongside some arguments in a Curl POST. Is this possible? For now, I only send files. This is the POST that I have:

curl  -X POST  -i  -F parametername=@/Users/mymachine/Desktop/file.vcf localhost:5000

and this is how I retrieve it on the server that runs in my android app:

             val inputStream = exchange.requestBody

            try {
                val inputAsString = inputStream.bufferedReader().use { it.readText() }
                Timber.d("Received a POST $inputAsString")

            } catch (e: Exception) {
                Timber.e("Error while attempting to parse server response ${e.message}")
            }

I created the local server following this example: https://medium.com/hacktive-devs/creating-a-local-http-server-on-android-49831fbad9ca

Is it possible to wrap the file that I'm sending in a JSONObject and add more fields? Something like this is what I'm after:

curl  -X POST  -i  -F {"vcf_file":"parametername=@/Users/mymachine/Desktop/file.vcf", "name":"Jhon", "surname":"Doe"} localhost:5000

If its not possible to wrap everything in a JSONObject...what would be to best approach? Thank you in advance!

android enthusiast
  • 2,062
  • 2
  • 24
  • 45

2 Answers2

0

Yes, I think provided you hosted your api / files you can easily get their URLs and place them in the json. But if you want to do an upload for example, you should do a multipart request. Retrofit will definitely be a big help for you, here are some links:

https://square.github.io/retrofit/

https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter

https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server

Mike
  • 1,313
  • 12
  • 21
  • Also do note, you will need permissions to read and write access storage if it will be coming from your android device. – Mike Apr 08 '20 at 10:24
  • Perhaps I didn't explain the issue very well. The problem I'm facing in not in the android app but in the backend – android enthusiast Apr 08 '20 at 15:43
0

I've done more investigation and found the answer here

send/post xml file using curl command line

My Curl post looks like this now:

curl -X POST --form details='{"bar_test":"bar", "foo": "bar"}' --form  parametername=@/Users/myusername/Desktop/test.vcf  localhost:5000
android enthusiast
  • 2,062
  • 2
  • 24
  • 45