Reputation: 5
Im trying to post data with retrofit with jwt bearer authentication, but when i debug response
code, its
error 400
, and the message is body = null
This is my Intefrace class
@Headers({"accept: text/plain","Content-Type: application/json-patch+json" })
@POST("services/app/ProjectService/Create")
Call<ProjectRequest> postProject(@Header("Authorization") String token,
@Body ProjectRequest projectRequest
);
This is my model class
public class ProjectRequest {
@SerializedName("name")
@Expose
private String name;
@SerializedName("description")
@Expose
private String description;
@SerializedName("ownerId")
@Expose
private int ownerId;
@SerializedName("startPeriod")
@Expose
private String startPeriod;
@SerializedName("endPeriod")
@Expose
private String endPeriod;
@SerializedName("isDraft")
@Expose
private boolean isDraft;
@SerializedName("ownerName")
@Expose
private String ownerName;
@SerializedName("pitId")
@Expose
private int pitId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getOwnerId() {
return ownerId;
}
public void setOwnerId(int ownerId) {
this.ownerId = ownerId;
}
public String getStartPeriod() {
return startPeriod;
}
public void setStartPeriod(String startPeriod) {
this.startPeriod = startPeriod;
}
public String getEndPeriod() {
return endPeriod;
}
public void setEndPeriod(String endPeriod) {
this.endPeriod = endPeriod;
}
public boolean isDraft() {
return isDraft;
}
public void setDraft(boolean draft) {
isDraft = draft;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public int getPitId() {
return pitId;
}
public void setPitId(int pitId) {
this.pitId = pitId;
}
And the main class is
private void initApi() {
projectApi = ApiClient.getClient().create(ProjectApi.class);
}
private void postProject(final boolean isOnline) {
if (isOnline) {
final ProjectRequest projectRequest = new ProjectRequest();
projectRequest.setName(projectName);
projectRequest.setDescription(description);
projectRequest.setOwnerId(1);
projectRequest.setStartPeriod(dateStart);
projectRequest.setEndPeriod(dateEnd);
projectRequest.setDraft(true);
projectRequest.setOwnerName("yogi kun");
projectRequest.setPitId(1);
Call<ProjectRequest> callProject = projectApi.postProject(" Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6Il and so . . . ",
projectRequest);
callProject.enqueue(new Callback<ProjectRequest>() {
@Override
public void onResponse(@NotNull Call<ProjectRequest> call, @NotNull Response<ProjectRequest> response) {
if (response.isSuccessful()) {
try {
Toast.makeText(getContext(), "Success", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
String message = "";
if (e.getMessage() != null) {
message = e.getMessage();
}
Toast.makeText(getContext(), "Failed " + message, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getContext(), "Failed " + response, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(@NotNull Call<ProjectRequest> call, @NotNull Throwable t) {
Toast.makeText(getContext(), "Failure" + t, Toast.LENGTH_SHORT).show();
}
});
}
And the error is
And
Upvotes: 0
Views: 211
Reputation: 66
Might be an issue with how the JSON is formatted, compared to what you're expecting. Are you able to use something like Postman and try this API call to see the format it comes back? Can't tell that much but the error message shown is finding some array somewhere in the JSON
Upvotes: 1