How to pass JSON as a query parameter in a HTTP request? - rest

I am using RESTEasy stack to implement a REST based client and server. I have a service which handles POST request, like below:
#POST
#Path("/this")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String testPost(#QueryParam("thing") String thing) {
...
}
When I call from Postman or REST client the following URI:
http://ip:port/base/this?thing={"id":"abc"}
I always get the error as
java.net.URISyntaxException: Illegal character in query at index (pointing to =)
What is the reason for this?

If you're trying to parse a POST request, you should use #FormParam instead of #QueryParam. Then, you can make the post request doing something like this (using curl)
curl -i -X POST -H "Content-Type: application/json" -d thing="{\"id\":\"abc\"}" http://ip:port/base/this

Related

How to upload file in RestAssured for PUT (non-multipart) request

I'm working on automating the PUT API which expects file to be sent as InputStream. We used curl request to test this locally and now need to automate using RestAssured. I went through the documentation of RestAssured RequestSpecBuilder.setBody() which works only for POST methods and doesn't work for PUT.
Any pointers on how to upload file to PUT API using RestAssured would be helpful.
Below are details :
Curl command used curl -v --location --request PUT 'http://localhost:1080/update' "${HEADERS}" --upload-file <<FILE_PATH>>
API Definition :
#PUT
public Response updateResource(#Context final HttpServletRequest httpServletRequest,
#NotNull final InputStream inputRequestBody) {
// Do processing
}
given().multiPart() is used to upload a file.
The syntax will be similar to:
given().multipart("createJS", new File(D:\\Automation\\filepath"), "application/json")
You will need to change path and file type.

What is the correct way to test an API with token api_key:access_token header?

Following the below steps:
created a new app at https://developers.kite.trade/apps
obtained the <API key> from the app details page
obtained the <API secret> from the app details page
calling an API called holdings API using curl like this:
curl -X GET https://api.kite.trade/portfolio/holdings -H 'Authorization: token <API key>:<API secret>' -H 'X-Kite-Version: 3'
All the steps look correct to me however I'm getting the following error:
{
"status": "error",
"message": "Incorrect `api_key` or `access_token`.",
"data": null,
"error_type": "TokenException"
}
I regenerated the <API secret> 3 times from the app details console.
Now, the question here is not about why I'm getting the error from https://api.kite.trade
The question is whether the authorization header is correct or not?
I have seen many APIs asking for base64 encoded headers so I did that too but the API seems not working.
Is it not the right approach for testing an API?
Try using Postman to test your API request. Also, check what kind of authentication your API is using ( oAuth 2.0, etc). If that's the case, your request headers might look something like this:
{
Authorization: 'Bearer <API token>'
}

CumulusRDF Update Query

I'm trying to insert data my CumulusRDF via REST.
Problem: If i'm doing a query, it works nice, with the GET method
Ex: A simple query:
select *
where
{
?s ?p ?o.
}
GET method:
curl -X GET --header 'Accept: application/sparql-results' 'http://localhost:9090/cumulus/sparql?query=select+*+where+%7B%3Fs+%3Fp+%3Fo.%7D&accept=text%2Fhtml'
But, when i try to insert data, it fails.
Query:
PREFIX dc: <http://purl.org/dc/elements/1.1/>
INSERT DATA
{
<http://example/book1> dc:title "Harry Potter".
}
POST method:
curl -i -X POST -H 'Content-Type: application/sparql-query' 'http://localhost:9090/cumulus/sparql?query=PREFIX+dc%3A+%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Felements%2F1.1%2F%3E%0AINSERT+DATA%0A%7B+%0A++%3Chttp%3A%2F%2Fexample%2Fbook1%3E+dc%3Atitle+%22Harry+Potter%22.%0A%7D
Erro:
HTTP/1.1 400 Bad Request
What i'm doing wrong? CumulusRDF is instaled in a Docker with Apache Cassandra DB.
In SPARQL queries are not updates. There are two separate languages.
If you use the HTTP query string, content-type is irrelevant. It should be?update=.
Better is to POST with the update in the HTTP body and using
Content-Type: application/sparql-update

Jersey REST GET is working but PUT not. The specified HTTP method is not allowed for the requested resource

I've been breaking my head over this for a few days now. This little sniplet is working fine (using Jersey 2.26-b03 on Tomcat).
#GET
#Path("/{code}")
public Response update(#PathParam("code") String code) {
System.out.println("!!!!!!!");
return Response.status(Response.Status.OK).build();
}
curl -i -X GET http://localhost:18270/nyx/rest/servervirtueel/SVM0000
HTTP/1.1 200 OK
Followed by a bunch of Jersey tracing I enabled. But if I only change the GET to a PUT (exactly the same method, just change the annotation):
#PUT
#Path("/{code}")
public Response update(#PathParam("code") String code) {
System.out.println("!!!!!!!");
return Response.status(Response.Status.OK).build();
}
curl -i -X PUT http://localhost:18270/nyx/rest/servervirtueel/SVM0000
HTTP/1.1 405 Method Not Allowed
Followed by HTML telling me that the "The specified HTTP method is not allowed for the requested resource". However, POST does work (changing the annotation again).
It turned out that the OWASP method whitelist valve was configured on Tomcat (Catalina) level to only allow GET and POST; this is a webapp that held only SOAP services until now. You do not see this in either web.xml's or server.xml, but it's in Catalina/localhost/webappname.xml.

Couchbase create document fails through sync-gateway public rest API

As per Couchbase Sync-Gateway REST API documentation here below mentioned cURL should create a document in the specified database.
Below is the generated cURL from Postman.
curl -X PUT -H "Cache-Control: no-cache" -H "Postman-Token: 498d0fb6-77ac-9335-2379-14258c6731c7" -d '' "http://192.168.244.174:4984/db/"
I also tried adding JSON to the body of the request.
But when I send the put request through Postman, instead of creating a new document, it tries to create a new database and the JSON response is
{
"error": "Precondition Failed",
"reason": "Database already exists"
}
Am I missing something or it was a bug? Is there any other way to create a document to sync gateway?
There is a mistake in the documentation.
As per documentation,
You can either specify the document ID by including the _id object in the request message body, or let the software generate an ID.
But Couchbase REST API does not seem to work like that (may be they are not updating their documentation regularly). You need to provide the id in the URL like /{db}/{id}.
The below cURL worked for me.
curl -X PUT -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 75ab844e-5130-708e-69e9-e87f878108b4" -d '{"name": "xxx",
"full_name": "xxx yyy"}' "http://192.168.244.174:4984/db/123"
JSON response is
{
"id": "123",
"ok": true,
"rev": "1-9324dabc947fc963a754b113d1215ac3"
}