I have code :
InterfaceAPI:
#GET("/api/getUserAsset?")
Call<MyObject> getUserAsset(#Query("user_id") String userId);
The Library said : HTTP OK 200
MyObject.java :
#SerializedName("name")
private String name;
JSON Response from API :
[{"name":"bob"}]
When i call the method getUserAsset i always get NULL, why?
Your JSON response is an array.
it should be : {"name" : "bob"} if you want to be able to deserialize it correctly.
Related
Request body is:-
{
"request":
{
"body":
{
"ABC":["XYZ"],
"PASSWORD": "password"
}
}
MY CODE:-
I am getting error in response because the ABC value is going in string format
Map body = new LinkedHashMap(6);
body.put("ABC","XYZ");
body.put("PASSWORD","password");
what is the correct way to write this request body using LinkedHashMap in restAssured
"ABC":["XYZ"],
You just need change it to List<String>
body.put("ABC", Arrays.asList("XYZ"));
My JSON Response reads something like:
{
"_embedded":{
"contents": [
{
"data": 1234,
"success": true,
}
]
}
}
I am attempting to extract the the success message and data. However my console output keeps reading null.
Once extracting the Response here is my code that receives a Null response using Rest Assured:
String res = response.asString():
JsonPath js = new JsonPath(res);
String success = js.get("_embedded[0].contents[0].success");
String data = js.get("_embedded[0].contents[0].data");
System.out.println(success);
System.out.println(data);
My response for both success and data is null
From your JSON sample, it looks like _embedded is not a list. _embedded[0] might return null because there is no list named _embedded when you try to extract the success value using "_embedded[0].contents[0].success".
Extract the success value by using
js.get("$._embedded.contents[0].success");
I have this request and I need to send it by FormUrlEncoded using Retrofit
{
"clnt_id": "OQW",
"clnt_res": "AA!##$T",
"type": "SCDS",
"len": "ASD"
}
I used this code:
#FormUrlEncoded
#POST("Endpoint")
#Headers("Accept: Application/JSON")
fun connect(
#Field("clnt_id") clnt_id: String,
#Field(value = "clnt_res", encoded = false) clnt_res: String,
#Field("type") type: String,
#Field("len") len: String
): Observable<Token>
First, thing is that the request is not sent as JSON
Second, the value of "clnt_res", encoded by retrofit
You have 2 options to send json request from android using Retrofit.
Create Pojo Model of json request and pass it by setting values of it.
Create HashMap and pass it to request.
Here is solution using 2nd Method:
Create hashmap and put key(parameters) and value:
Map<String,String> requestMap = new HashMap<>();
requestMap.put("clnt_id","your_value");
requestMap.put("clnt_res","your_value");
requestMap.put("type","your_value");
requestMap.put("len","your_value");
Then pass it to your retrofit request using FieldMap:
#FormUrlEncoded
#POST("Endpoint")
#Headers("Accept: Application/JSON")
fun connect(
#FieldMap requestMap:Map<String,String>
): Observable<Token>
I finally get the answer and it was a problem with symbol '$' in 'clnt_res' value "AA!##$T", The problem is in kotlin to escape a special char you need to do this "\$", what I made the IDE didn't tell me that it is wrong is this "$/".
I am facing an issue while making a request body to do an API call in Java.
Required Body
{
"id" : [1,2]
}
I have an integer array with me lets say arr, I am creating the request something like:-
JSONObject jsonObject = new JSONObject();
jsonObject.put("id",Arrays.toString(arr));
String stringBody = jsonObject.toJSONString();
RequestSpecification specification = RestAssured.with();
specification.body(stringBody);
Response response = specification.post(endpoint);
What it actually does is make the request body as something like below.
{
"id" : "[1,2]"
}
As it sends the value as String so my server throws an error, Expected a list of items but got type \"unicode\".
Can somebody help me in here. How do I send it in raw format instead of String.
Use
jsonObject.put("id",Arrays.asList(arr));
to build the json body.
I have a nested JSON response. JsonResponse Screenshot
I want to get the the dictionary at 0th location from the list and then get a particular element from it.
For e.g. In response, {0} and {1}, I want to get complete {0} as a result. Then from {0}, I want to extract "Id" value only.
I don't want to use the JsonPath.read(JsonResponse String, JSON Path) everytime. So looking for some simple and better alternative.
How to convert JSON response to Java List. Below is the response.
Response resp = given().header("Authorization", "Bearer "+"dwded").
accept(ContentType.JSON).
when().
get("https://example.com");
return resp;
For testing a web-API or REST endpoint, I would recommend Karate.
So it becomes simple:
* def id = response[0].Id
In the swagger editor pet example.
responses:
200:
description: "successful operation"
schema:
type: "array"
items:
$ref: "#/definitions/Pet"
A model is generated from the
Pet:
type: "object"
properties:
name:
type: "string"
example: "doggie"
This generated a java class
public class Pet {
#JsonProperty("name")
private String name = null;
The api shows a REST that returns an entity that can be shown as an array of json objects
ResponseEntity<List<Pet>> findPetsByStatus( #NotNull#ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "available, pending, sold") #RequestParam(value = "status", required = true) List<String> status);