Reputation: 1
I have POST API, i'm using retroFit in my android source code, this is API parameter in POSTMan this is header: enter image description here
This is Body:
Thisis my pseudo Code Android, API interface:
@FormUrlEncoded
@Headers({"Content-Type: multipart/form-data", "Authorization: F31daaw313415"})
@POST("api/store/order")
Call<List<PostCommandResponse>> setCommandeProduct(@FieldMap Map<String, Object> test);
This is my Map
data.put("product_id",productId);
data.put("adress", RequestBody.create(MediaType.parse("multipart/form-data"),adresse));
data.put("city",RequestBody.create(MediaType.parse("multipart/form-data"),city));
data.put("zip",RequestBody.create(MediaType.parse("multipart/form-data"),zip));
data.put("first_name",RequestBody.create(MediaType.parse("multipart/form-data"),surname));
data.put("last_name",RequestBody.create(MediaType.parse("multipart/form-data"),lastname));
But always the response is 400 bad formated request.
Upvotes: 0
Views: 386
Reputation: 11018
My suggestion would be to use the data model class something like below.
public class UserInfo {
@SerializedName("product_id")
@Expose
private Integer productId;
@SerializedName("address")
@Expose
private String address;
@SerializedName("city")
@Expose
private String city;
@SerializedName("zip")
@Expose
private String zip;
@SerializedName("first_name")
@Expose
private String firstName;
@SerializedName("last_name")
@Expose
private String lastName;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
set the values in this model UserInfo
and pass this to the API call something like below.
@Headers({"Content-Type: multipart/form-data", "Authorization: Bearer F31daaw313415"})
@POST("api/store/order")
Call<List<PostCommandResponse>> setCommandeProduct(@Body UserInfo test);
Just a small correction: also check if the adress or address is needed to be sent in JSON. adress is getting used in the JSON body image. Change accordingly.
Edit: you are sending the headers in the wrong format also.
It should be something like this.
@Headers({"Content-Type: multipart/form-data", "Authorization: Bearer F31daaw313415"})
Upvotes: 1