Converting postman request to retrofit 2 - android

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.

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.

403 on Android for Reddit API but not on browser

When making a request with Retrofit using an OKHttp Client with interceptors to add headers:
(Authorization: Bearer + access_token)
(User-Agent: "user_agetn_as_described_by_reddit")
Using the oauth.reddit.com/.json URL as recommended by Reddit, I get a 403. Now, when I do this same thing on Postman or something similar, I get a 200 and the expected JSON.
Is anyone aware of something related to Android or Retrofit or OKHttp that could be causing me this pain?
I was using the auth code instead of the access_token to make my requests. A mistake from my side. You should use the auth code to make a token request.

Dealing with multipart responses

I'm playing with Amazon Alexa (AVS) and the service mostly sends multipart responses.
For example a response can contain an application/json part associated with an application/octet-stream (MP3 data) part.
For now I don't have any idea the way to write my Retrofit2 service method for it to handle it correctly.
I guess an AlexaMultiPartResponse object here won't work:
#Multipart
#Post("/path")
Call<AlexaMultiPartResponse> getAnswer(#Part("metadata") RequestBody metadata, #Part("audio") RequestBody audio);
Do you have any idea?
Regards.
According to this: https://github.com/square/retrofit/issues/2164 there is no elegant way to do it with Retrofit2.
So I ended up parsing the multipart response with Apache FileUpload MultipartStream (https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/MultipartStream.html)

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.

How to use Retrofit with Rails for Uploading Images to Server

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.

Categories

Resources