简体   繁体   中英

Not able to recreate a cURL POST request using okHTTP multipart-form data in Java for my android app

I have tried for two days now to understand how to replicate a curl request for adding an image to my Craftar image recognition collection. I am able to create an Item, but the way to setup the multipart-data code has failed me. The Craftar only provides the cURL statement of how to Post an image file to attach it to a collection Item.

I have tried using RequestB.in and Postman to see what I can see in the message transfer to figure it out, but no combination of the okHTTP methods seems to do the trick. I'm stumped. I have tried both "image/png" and "application/octet-stream" as my MediaTypes. RequestB.in showed "application/octet-stream" when I used Postman to send it a test request. Below is what the message info it showed.

Here is the cURL statement I'm trying to replicate with Java.

curl -X POST ' https://my.craftar.net/api/v0/image/api_key=123456789abcdefghijk123456789abcdefghijk ' -F "item=/api/v0/item/d05924dde1784787a3d6b815e2b1cd69/" -F "file=@back_cover.jpg"

Here is my code:

        public class OkHttpHandler extends AsyncTask<String, Void, String> {

        OkHttpClient client = new OkHttpClient();

        @Override
        protected String doInBackground(String... params) {

            File storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            File file = new File("/storage/emulated/0/Pictures/BlissBotSenderImages/kaytiandkristoffer.jpg");
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("item", "/api/v0/item/d05924dde1784787a3d6b815e2b1cd69/")
                    .addFormDataPart("file","kaytiandkristoffer.jpg",
                    (RequestBody.create (MEDIA_TYPE_PNG, file)))
                    .build();

            Request request = new Request.Builder()
                    .url(postImageUrl)
                    .post(requestBody)
                    .build();

            try {
                Response response = client.newCall(request).execute();
                return response.body().string();
            } catch (Exception e) {
                e.printStackTrace();
            }

Below are the RequestB.in successful Post from Postman message details:


FORM/POST PARAMETERS item: /api/v0/item/d05924dde1784787a3d6b815e2b1cd69/

HEADERS Cf-Ray: 3cdba305fb509b62-DFW

Via: 1.1 vegur

Cf-Visitor: {"scheme":"https"}

Content-Type: multipart/form-data; boundary=a11df5d0-f007-4d3a-9849-18fe334c532c

Accept-Encoding: gzip

Content-Length: 3509853

Total-Route-Time: 0

Cf-Connecting-Ip: 2001:5b0:42c4:2ae8:2954:59ea:a3e8:4787

X-Request-Id: 766063f7-c368-4467-b5ed-42776ac6f528

Host: requestb.in

Connection: close

Cf-Ipcountry: US

User-Agent: okhttp/3.9.1

Connect-Time: 1

RAW BODY --a11df5d0-f007-4d3a-9849-18fe334c532c Content-Disposition: form-data; name="item" Content-Length: 46

/api/v0/item/d05924dde1784787a3d6b815e2b1cd69/ --a11df5d0-f007-4d3a-9849-18fe334c532c Content-Disposition: form-data; name="file"; filename="kaytiandkristoffer.jpg" Content-Type: application/octet-stream Content-Length: 3509467

ÿØÿáExifII*
¦°¸(1 À2ÎiânSAMSUNGSPH-L720THHL720TVPUBNG52015:04:05 21:22:25\\d"'20220l

¤ ¬´
¼|bîÄ 0100 P¢£¤¤¤¤ ¤!ÌåÜd2015:04:05 21:22:252015:04:05 21:22:25ìäd2 äd¤dASCIINVMU^^;YgZHXgmGum{wsxÞ¾ï2ì2åÞ¾ïso|Þ¾ïÞ¾ïs|Þ¾ïUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU~Þ¾ïxt¡vuÞ¾ïCALCur|0xDD70utvp{vVigcjijHrimoaJi[pnL sEX \\?;I99:C9SHxVBS?@4 9?<>Þ¾ïTUNAIFJ"BFPMHERC=7->98'VNC-/=E7AD35IECLI:P[aBDVabVTiZZ:@HHG[EH[LZ8RLLn< 8a=FD"GONLNZTNGBQQLPGR _޾ﮮ®®Fèþ»)q5èêÈìµ×2þJ +'#ÿê%:pF}-©}½-©.^2@E×û »'%('%$.!!$$#&*@Þ¾ï "# E-&3>-JR>GSnxfZcZ[U]R]]W^SOXM¨a~ziqkfeheUY[nzzvra~eh[Rh\\belL[NGI>RNbddqmIAIEIGNRNAAD59<-4':ZD/#.BA8$$$@8JG\\qgZ~{qmcy[\\~rhttcio xtne^y{{r~vmxr|rsweu{gw{iqs~ozgvy|xxffwnyyZk|WW yvt{|s}wz{zw~|vvxsjquxyykdv}jtp{}{ryY~vv}tqx{pgjsolzq~ftxrkug{|Sjqn|w]kwlq}mssij~r|||zn|}w|u{j~tmHztMvoBZlV Gt[aUp]>? ,306A7$!BB13-

(60,!%6H@ELEB@6%KVJ@t;04 OFC4- '# $*71.;^M@IVTMF@L>dkL,:KE7<%$?MxIu ®crZs/Y7Z/DDS_WVRgT^ VGPb^gTORN4%Lo |p«¼ÇÓÎÄÅÃô¬°¦qg?NOa\\rl\\T N;XL"e5F=H=ASlOjTwd\\dclSVXsnXXWYcG7B-XTK:cabfgfiitnW/$5)E28512*1$1M+4G@8/BC3,4 '<,12='/+-3%2,A()')&.+'6.&C?),1@/52$-%8;93(-+ ),,#.=+%**+ '6$51("@'K /, #!%+!("#&$,+($)!%88 !.-0&&$'!#+'&$(5/ ,) ;---0 5,2!+& "0/( &") $+<&(#$" +/&"# 9* #$&$"%"$$!!#&/0*( $$ ('!$&"#.1&

as sometimes happens the problem lay elsewhere. somewhere along the line I accidentally inserted an extraneous character in the endpoint address. I fixed that and the above code works just find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM