I pass this data {"id:","1234} via JsonObject like this:
JsonObject json = new JsonObject();
json.addProperty("id", "1234");
but I keep getting 403, I wonder if retrofit can read the JsonObject
My request:
@Headers("Content-Type: application/json")
@POST("/user")
request(@Body JsonObject body);
I also tried to create my object:
public class MyId {
private String id;
public MyId(String id) {
this.id = id;
}
}
then:
MyId id = new MyId("1234");
request(id);
request:
@POST("/user")
request(@Body MyId body);
instead of JsonObject use Map
Map<String, String> requestBody = new HashMap<>();
requestBody.put("id", "1234");
request:
@Headers("Content-Type: application/json")
@POST("/user")
request(@Body Map<String, String> body);
JsonObject will work fine (assuming you are using Gson). A 403 is a forbidden response, which just indicates that the URL or structure of the request is to a place that you're not allowed to access.
This isn't a Retrofit bug, just a disagreement between where the server expects you to send data and where you are actually sending it. If you reconcile that it should start working.
How was the issue fixed? ?Above mentioned methods didn't work for me
@Osfalt @usmanrana07 it works for me.
fun xunfei(content: String,
from: Int = 1,
to: Int = 2): Observable<XFResp> {
return HELPER.xunfei(HashMap<String, String>().apply {
put("content", content)
put("sourceLanguage", from.toString())
put("targetLanguage", to.toString())
put("appId", XF_APP_ID)
})
@POST(TranslateHelper.XUNFEI_URL)
@Headers("Accept: application/json", "Content-Type: application/json")
fun xunfei(@Body body: Map<String, String>): Observable<XFResp>
ps: you should also check your Interceptor
what if i have a big json somthing like below, the how to send it in body , as it creates three model classes and its a nested json , which is cumbersome to add each value and make jsonobject of it.
{
"gender": "MALE",
"email": "string",
"name": {
"firstName": "string",
"lastName": "string"
},
"timezone": "string",
"userNameType": "PHONE_NUMBER",
"phone": {
"type": "MOBILE",
"manufacturer": "string"
},
"language": "en",
"password": "string"
}
Most helpful comment
instead of JsonObject use Map
request: