I am trying to send data that is in this form
{
"id": ,
"venue": {
"id": ,
"name": "",
"city": "",
"address": "",
"rating": null,
"point": null
},
"name": "",
"time": "",
"event_pic": null,
"description": "",
"event_type": "Movie",
"invite_only": ,
"free": ,
"age_restriction": ,
"ticket_price": ,
"user":
}
I have created the api interface like this:
```
@Multipart
@POST("api/events/")
Observable
@Part("venue") RequestBody venue,
@Part("event_pic") RequestBody image,
@Part("name") RequestBody name,
@Part("description") RequestBody description,
@Part("time") RequestBody date,
@Part("event_type") RequestBody type,
@Part("invite_only") RequestBody isInviteOnly,
@Part("age_restriction") RequestBody isAgeRestricted,
@Part("free") RequestBody isFree,
@Part("ticket_price") RequestBody ticketPrice
);
Gson gson = new Gson();
MediaType json = MediaType.parse("application/json");
MediaType image = MediaType.parse("image/*");
MediaType text = MediaType.parse("text/plain");
String venue_json ="venue:"+ gson.toJson(venue)+"";
File file = new File(path);
RequestBody requestBodyVenue = RequestBody.create(json, venue_json);
RequestBody requestBodyImage = RequestBody.create(image, file);
RequestBody requestBodyName = RequestBody.create(text, name);
RequestBody requestBodyDate = RequestBody.create(text, date);
RequestBody requestBodyDescription = RequestBody.create(text, description);
RequestBody requestBodyType = RequestBody.create(text, type);
RequestBody requestBodyAge = RequestBody.create(text, ageRestricted);
RequestBody requestBodyFree = RequestBody.create(text, free);
RequestBody requestBodyInviteOnly = RequestBody.create(text, inviteOnly);
RequestBody requestBodyTicketPrice = RequestBody.create(text, ticketPrice);
return apiService.postEvent(requestBodyVenue, requestBodyImage, requestBodyName, requestBodyDescription,
requestBodyDate, requestBodyType, requestBodyInviteOnly, requestBodyAge, requestBodyFree, requestBodyTicketPrice);
```
However it does not send the nested venue json object. Is this supported in retrofit?
You need to create an object that represents the entire JSON object and use that as the sole @Body argument. Consider looking at http://www.jsonschema2pojo.org/ to create it for you.
A multi-part request is something very different and is usually used for uploading files or submitting complex forms.
For general usage questions please use StackOverflow with the 'retrofit' tag. We monitor it and can respond to these types of questions on there.
A sample/link/blog for using the same would be helpful for the future visitors. Thanks
Here is sample log I have created for the same. But there is a problem in understanding request at the server end.
note: I don't have access to the server and it is completely hidden from me
```
@Multipart
@POST(NetworkConstant.TEACHER_SENT_MSG)
fun sendMessage(
@Header("auth_key") authKey: String,
@Header("code") code: String,
@Part part: MultipartBody.Part,
@Part file: MultipartBody.Part? = null
): Observable
05-02 01:29:55.153 24798-24866/app.errandel.android D/OkHttp: --> POST /teacher_desk.php http/1.1
Content-Type: multipart/form-data; boundary=a61cb46c-8f0e-43c9-8c11-5ef3b0c4bbd9
Content-Length: 236581
auth_key: e6d50226483e9bfabf62a52cfdcb0196
code: 1001
05-02 01:29:55.167 24798-24866/app.errandel.android D/OkHttp: --a61cb46c-8f0e-43c9-8c11-5ef3b0c4bbd9
Content-Type: application/json; charset=utf-8
Content-Length: 89
05-02 01:29:55.168 24798-24866/app.errandel.android D/OkHttp: {"admission_no":["P97-10-22"],"file_type":["2"],"message":"ddwd","title":"ss","type":"1"}
--a61cb46c-8f0e-43c9-8c11-5ef3b0c4bbd9
Content-Disposition: form-data; name="files"; filename="voice_message.wav"
Content-Type: multipart/form-data
Content-Length: 236160
RIFF����WAVEfmt �������������������
05-02 01:29:55.212 24798-24866/app.errandel.android D/OkHttp: --> END POST (236581-byte body)
05-02 01:29:56.090 24798-24866/app.errandel.android D/OkHttp: <-- 400 Bad Request /teacher_desk.php (878ms)
```
good
@zmwas @JakeWharton Is it possible to save simple json string/object to gist using Retrofit ? If yes please share somecode snippet as an example .
@zmwas : did you find the solution, if so please share me the code sample, I am stuck with the same issue
I have a list like
[{
Imgstr:"obj as string",
Ingfile:file
},{
Imgstr:"obj as string",
Ingfile:file
}
]
How can I upload them as multipart x if it is a single obj I am sending as multipart body and file
{"data": {
"bookDate": "2021/03/01",
"bookTime": "4:57 PM",
"createdBy": {
"firstName": "SaNjEeT",
"role": "patient",
"userId": "603cb8bd7a6f8018d09b2d15",
"userName": "80055885868"
},
"currentlocation": {
"address": "C 26, C Block, Sector 65, Noida, Uttar Pradesh 201301, India",
"address1": "Ezimax Ltd.",
"address2": "Ezimax",
"coordinates": [
28.6104975,
77.38590529999999
],
"pincode": "201301",
"type": "Point"
},
"emergencyContactNumber": "80045435868",
"emergencyContactPerson": "SaNjEeT KuMaR",
"isActive": true,
"isCancelled": false,
"isDeleted": false,
"isPaid": false,
"patient": {
"address": "C 26, C Block, Sector 65, vt. Ltd., Ezimax, 201301",
"addresses": [{
"latitude": 28.6104975,
"longitude": 77.38590529999999
}],
"email": "[email protected]",
"firstName": "SaNjEeT",
"lastName": "KuMaR",
"mobile": "8003422868",
"patientId": "603cb8bd7a6f8018d09b2d15"
},
"pinCode": "201301",
"requestBy": {
"email": "[email protected]",
"firstName": "SaNjEeT",
"lastName": "KuMaR",
"mobile": "80wqq05868",
"userId": "603cb8bd7a6f8018d09b2d15"
},
"serviceName": "Nursing",
"serviceType": "Health",
"service_id": "5f57380b2fbbe93ce527553a",
"subServiceId": "102",
"subServiceName": "injection"
}}
how to send this object in multipart form data using retrofit and i have to send multiple images with this object
Most helpful comment
You need to create an object that represents the entire JSON object and use that as the sole
@Bodyargument. Consider looking at http://www.jsonschema2pojo.org/ to create it for you.A multi-part request is something very different and is usually used for uploading files or submitting complex forms.
For general usage questions please use StackOverflow with the 'retrofit' tag. We monitor it and can respond to these types of questions on there.