I am trying to pass api key in restassured. From postman, i got the repsonse data using 'X-Api-Key' with value. But with rest assured, i am seeing null pointer exception
Response res = given()
.header("key","55af7bc105c9c2ee98e2abb32979fee")
.when()
.get ("https://mbaseurl/api/v1/search-listings/").then().
// contentType(ContentType.JSON). // check that the content type return from the API is JSON
extract().response();
I tried passing key value with .header("X-Api-Key", "value") but was not successful
Map<String, String> pMap = new HashMap<String, String>();
pMap.put("apiKey","yourAPIKEY");
pMap.put("format","json");
pMap.put("show","storeId,`storeType`,name,city,region");
Related
I'm triggering the Zomato api for the collections request, however, I keep getting the headers as if they were text/html. Postman returns them as JSON, which is what I need. All other Zomato apis I've tested so far are returning JSON but collections and not sure why. That's what I have on my attempts to force the JSON as the type for the response.
#Test
public void testGetCousinesApiReturnsItemsInAscendingAlphabeticalOrder() {
Map<String, Object> map = new HashMap<>();
map.put("city_id", 61);
Response r = given()
.baseUri(baseUrl)
.basePath("/cousines")
.queryParams(map)
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.contentType("application/json\r\n")
.header("Accept", "application/json").and()
.header("Content-Type", "application/json")
.header("user-key", zomatoKey)
.log().body(false)
.get();
System.out.println(r.getContentType());
}
I convert response to JsonPath :
public static JsonPath getJsonPath (Response res) {
String json = res.asString();
System.out.println("returned json: " + json);
return new JsonPath(json);
}
and then use this JsonPath to get each value of Response:
JsonPath jp = getJsonPath(res);
String type = jp.get("type");
We want to sent a post request to an external API provider.
we know how to send a GET, and how to parse the JSON response.
We know how to send a JSON payload into the POST.
What we cant find an example of a good way to get request parameters into the body of a POST with jersey.
e.g. to send a really simple get request, we can do this:
private final Client theHttpClient;
ClientConfig clientConfig = new ClientConfig();
JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
jacksonProvider.setMapper(theObjectMapper);
clientConfig.register(jacksonProvider);
clientConfig.register(EncodingFilter.class);
clientConfig.register(GZipEncoder.class);
theHttpClient = ClientBuilder.newClient(clientConfig);
int param1 = 123134
String param2 = "this+is+a+test";
String url = "https://api.some.com?param1=" + param1 + "¶m2=" + param2;
uri = new URI(url);
WebTarget webTarget = theHttpClient.target(uri);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
RESPONSE response = invocationBuilder.get(responseClass);
This code will send the params via get.
How would we get the params into a post request body?
we see there is an invocationBuild.post which looks like this:
#Override
public <T> T post(final Entity<?> entity, final Class<T> responseType)
throws ProcessingException, WebApplicationException {
return method("POST", entity, responseType);
}
What is entity in this case? We assume we could manually create a text body with the params packed in it, but there must be a nicer way to do this, which will ensure no typos etc? E.g. something which takes a map, or even an addParam function?
I am trying to send cypher query with parameters from .net to neo4j server with Rest method
i get this error :
Problem accessing /db/data/cypher. Reason:
java.lang.String cannot be cast to java.util.MapCaused by:
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map
This is the Code :
DateTime startQueryTime = DateTime.Now;
RestClient restClient = new RestClient("http://localhost:7474/db/data/cypher");
restClient.AddDefaultHeader("Accept", "application/json");
restClient.AddDefaultHeader("Content-Type", "application/json");
JObject parameters1 = new JObject();
parameters.Add("startName", "Alon");
RestRequest restRequest = new RestRequest(); ;
restRequest.AddParameter("query", "START root=node:Node_Type(Type=\"Person\") where root.Person_Name = {startName} RETURN root limit 20");
restRequest.AddParameter("params", parameters1);
IRestResponse restResponse = restClient.Post(restRequest);
thanks in advance.
Alon
The result of your query is of the form Map<String,Object> instead of String. The result map contains the node property names as keys and its values represented as objects.
Examine your actual REST outgoing call and make sure the parameters map is not serialized to a String but a JSON Map structure.
Is there any reason that the Breeze Web API implementation of the response to any metadata requests returns a string instead of a JSON object?
Sending metadata as text adds a lot of overhead over the network (due " encoding) and on clientside due manual JSON.parse.
I think that your controller can simply return the Metadata as JSON by specifying the contentType header:
i.e.
[HttpGet]
public HttpResponseMessage Metadata()
{
var result = new HttpResponseMessage { Content = new StringContent(_contextProvider.Metadata())};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return result;
}
As of v 1.2.7, the BreezeController attribute now does this automatically.... and thanks for the idea.
Having a string containing the following raw Json data (simplified for the sake of the question):
var MyString = "{ 'val': 'apple' }";
How can I create a JsonResult object representing MyString?
I tried to use the Json(object) method. but it handles the raw json data as an string -logically :P-. So the returned HTTP response looks like:
"{ 'val': 'apple' }"
instead of the given raw Json Data:
{ 'val': 'apple' }
this is what I want to achieve:
The Json() method on Controller is actually a helper method that creates a new JsonResult. If we look at the source code for this class*, we can see that it's not really doing that much -- just setting the content type to application/json, serializing your data object using a JavaScriptSerializer, and writing it the resulting string.. You can duplicate this behavior (minus the serialization, since you've already done that) by returning a ContentResult from your controller instead.
public ActionResult JsonData(int id) {
var jsonStringFromSomewhere = "{ 'val': 'apple' }";
// Content() creates a ContentResult just as Json() creates a JsonResult
return Content(jsonStringFromSomewhere, "application/json");
}
* Starting in MVC2, JsonResult also throws an exception if the user is making an HTTP GET request (as opposed to say a POST). Allowing users to retrieve JSON using an HTTP GET has security implications which you should be aware of before you permit this in your own app.
The way I have generated json data from a string is by using JavaScriptResult in the controller:
public JavaScriptResult jsonList( string jsonString)
{
jsonString = "var jsonobject = new Array(" + jsonString + ");";
return JavaScript(jsonString)
}
Then when you request pass the json string to that action in your controller, the result will be a file with javascript headers.
I think you can use the JavaScriptSerializer class for this
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = js.Deserialize("{ 'val': 'apple' }", typeof(object));