How to use Retrofit with Rails for Uploading Images to Server - android

I want to use Retrofit for Uploading Image to server with some Strings. As in Rails the tags are like
user[username],user[email],user[password]
So after searching a lot i found some where on Github issues of Retrofit to use
Call<UploadImageResponse> uploadImage(#Query("user[name]") String username,#Query("user[password]") String password,#Query("user[phoneno]") String phoneno,#Query("user[email]") String email,#Query("user[avatar]\"; filename=\"profilepic.png\" ") RequestBody file);
The #Query worked for me. But for image upload using #Query did't worked. I also Tried #Part but not got any success. Can anybody please Help me. I m a Fresher and using this techniques to save time and all the Boiler plate code.
I also Tried the replace in #Query and using #Multipart and #Part with String upload but it give Internal Server Error. Error 500.
when the Keys were simple like email and password in login then i use Map and pass them in Body
#POST("login.json")
Call<Model> model(#Body Map<String,String> user);
I hardcodedly pass the Keys and Value in Map and it also worked.
So can any Body please tell me how to Upload Image using Retrofit to server with Key look like
user[avatar]
in Body.
I can upload image using Postman to server so Server is working Properly.

Related

how to send retrofit request body from postman when request body is just json

I'm send data to rest service using retrofit and it works but when server crash and i need to test the api in postman but when using postman data is null,i can see that data are delivered using dump and die but when trying to access it return null
#POST("adToCart")
Call<CartDataResponse> addToCart(#Body CartData cartData);
As your request body is CartData, you can just simply use Gson.toJson(cartData) where, if you're using google Gson libray with retrofit and cartData is an object of CartData.java or the request model class. Print that value of the json which you have got from Gson.toJson(cartData), copy the json and put it into the body portion of postman and make sure that you have fill the hearder and correct endpoint, Then hit 'send' button which will give you your expected response. If not please share your steps for confirmation.

I can’t send the image using the post request and from the Volley library in the Kotlin programming language

I’m developing a mobile application that sends photos to the server using a post request with a key. Do not get to send this very request. I would be grateful for any help!
The error "Com.android.volley.TimeoutError" is displayed in the application
error snapshot:
But why are you using Volley any specific reason ?? i suggest to use retorfit and with that user requestbody with multipart, it should help you.

Converting postman request to retrofit 2

I have some http request related problem which I am not able to solve.
I have aws url on which I have to make PUT request with mp4 video file to upload with header "Content-Type=video/mp4"
I wrote the request in postman and successfully uploaded the mo4 video file. Here is my postman screenshot.
I tried implementing it in Android with retrofit and successfully did it as well with 200 response from server. But the problem is, video file is not being uploaded in correct format and getting error in aws console.
Get this error: 4000 3f0fbcf1-1f39-4dc8-9d1e-332966662dd0: Amazon
Elastic Transcoder could not interpret the media file.
Here is my retrofit implementation
#PUT
#Multipart
fun postVideo(#Header("Content-Type") contentType: String
, #Url url: String
, #Part video: MultipartBody.Part): Call<ResponseBody>
And posting body as following
val requestBody = RequestBody.create(MediaType.parse("video/mp4"),videoFile)
val body = MultipartBody.Builder().addPart(requestBody).build()
As far as I know, I think my problem lies in the way I am attaching body to my request and header and url I am giving properly (as I am getting 200ok from server)
My lack of HTTP knowledge is giving me nightmares, any help is appreciated.

How to POST multiple large image files via Volley Android

I am trying to post around 10 images via volley by converting it to base64 but Volley post the images multiple time due to its retry policy. I am already compressing the files and i also tried changing its timeout but problem still exist. Retrofit also doing same.
Why do you need/use base64?
Are you using get rather than post?
Use multipart file upload for large files, for instance (with retrofit):
public interface FileUploadService {
#Multipart
#POST("/upload")
void upload(#Part("myfile") TypedFile file,
#Part("description") String description,
Callback<String> cb);
}
You can use Android Asynchronous Http Client for multipart request.
Refer this link
HttpClient support Base64.

Post With token_id and Json

Forgive me as I'm new to API driven development. I'm doing a front-end and back-end at the same time and ran into a snag.
On the back-end I accept URL encoded token_id that you may post to create a sessions. Goes like
http://someip:3000/sessions/create?token_id=sometoken
and works.
But now to create a post object the back-end expects a JSON object and a token_id. But reading some documentation for my front-end (android+retrofit) I understand that I can't URL encode a field (my token_id) and send a body as JSON.
Maybe I'm taking the wrong approach. Which path should I take to receive the token_id and a json object at the same time on the back-end?
I understand that I can't URL encode a field (my token_id) and send a
body as JSON.
This is true if you are trying to send POST parameters, because those are sent as the body. But you appear to be using query parameters which are part of the URL. You can use query parameters and JSON body in the same request. Your interface would be something like following, adjusting the body and return types for your particular case.
#POST("/sessions/create")
Call<Response> create(#Query("token_id") String tokenID, #Body MyBodyClass body);

Categories

Resources