Swagger Get API return HTML for static distribution bundle build using RestAssured instead of JSON Response. Works fine with CURL - rest-assured

Stuck with one issue which I am facing with RestAssured to validate one GET call on Swagger BookStore API. It’s working fine in Curl.
Working CURL command:
curl -X GET “https://bookstore.toolsqa.com/Account/v1/User/f00600d9-dc34-46e7-84f1-9404410b7f74” -H “accept: application/json” -H “authorization: Basic Qm9va1Rlc3RlcjEyOlRlc3RlciMxMg==”
AND it returns JSON response as expected.
Now, I am calling same API using RestAssured with code as below:
RestAssured.given().header(“accept”, “application/json”)
.header(“authorization”, “Basic Qm9va1Rlc3RlcjpUZXN0ZXIjMTI=”)
.when()
.get(“https://bookstore.toolsqa.com/Account​/v1​/User​/5641c9d9-1e3f-45be-a752-e1cc9d31e151”);
But it’s not giving any JSON response but HTML default page.

Maybe your code contains 'ZWSP​​' but you cannot see it on your IDE.
I saw it when copying your code to Intellij. Remove these, and the code worked fine.
RestAssured.given().header("accept", "application/json")
.header("authorization", "Basic Qm9va1Rlc3RlcjpUZXN0ZXIjMTI=")
.when()
.get("https://bookstore.toolsqa.com/Accoun/v1/User/5641c9d9-1e3f-45be-a752-e1cc9d31e151")
.prettyPrint();

Related

HTTP_ORIGIN always returning nil

I'm trying to get the origin of the request cross-site, I found this answer How to get "Origin" request header in Rails which suggest using
request.headers['origin']
I also tried
request.headers['HTTP_ORIGIN']
but both seem to return nil
I'm running this on Rails 6 app
There is nothing wrong with your code.
To get a header in Rails, just access it inside request.headers.
For example, to get User-Agent:
request.headers['User-Agent']
And since rails 5, we have the HTTP_ version also:
request.headers['HTTP_USER_AGENT']
The problem with the Origin header is that it does not always appear in request headers from the browser. It's just needed when the browser sending a cross-origin request.
You can try with some other tools like Postman or curl to see the header is really there:
curl -H "Origin: http://yourserver.com" http://yourserver.com

Getting JSESSIONID from JIRA using RestAssured throwing unknown host error

I have written a script using RestAssured to connect to Jira and get Session ID. Below is my code.
public void getJIRACookieTest(){
System.setProperty("https.proxyHost", "Host");
System.setProperty("https.proxyPort", "PortNo");
System.setProperty("https.proxyUserName","UserName");
System.setProperty("https.proxyPassword","Password");
Response res = given().header("Content-Type","application/json").
when().
body("{\"username\":\"test\",\"password\":\"test\"}").
post("https://jira_url/rest/auth/1/session").
then().extract().response();
System.out.println(res.prettyPrint().toString());
}
Here am getting unknow host error message.
Then i tried Postman and its perfectly working fine. I just selected no authentication, Content-Type as applicatio/json and body with user details
{\"username\":\"test\",\"password\":\"test\"}
in postman.
Not sure why am getting unknown host when i try with RestAssured.
Your help is really appreciated.
Thanks,
Narendra
From your comments I gather that you are connecting w/o a proxy using Postman and this works.
You are setting an HTTPS proxy in your RestAssured test anyway:
System.setProperty("https.proxyHost", "Host");
System.setProperty("https.proxyPort", "PortNo");
System.setProperty("https.proxyUserName","UserName");
System.setProperty("https.proxyPassword","Password");
You should be fine after removing these lines from your test.

Postman gives right response,but restassured returns empty for same request?

As you can see that postman returns expected result
but res.asString() gives [] in the blow code,can you tell me why?
def "simple test"(){
String url="http://xxx.xxx.xxx/assessment/api/Test.html"
when:""
io.restassured.response.Response res=RestAssured.given().header("Content-Type", "application/x-www-form-urlencoded").formParam("Action", "getDiagnosisList").formParam("Data", "[{\"subject\":\"冠心病\",\"option\":\"是\"}]").post(url)
then:""
res.prettyPrint()=="[\"身体健康状态不良\",\"医疗处置\"]"
}
It turns out that Chinese characters can't be encoded correctedly by default,after adding blow code,everything worked as expected:
RestAssured.given().config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().defaultContentCharset("UTF-8")))
Maybe the request did via postman has not been cached, and on the other hand the same request via restassured is using some kind of cache. Recently I was having a similar issue because of it was hitting the varnish server. I'd recommend you to take a look at the response headers from both postman and restassured.

How to correctly request a geoserver WFS via POST?

I have a geoserver instance, that contains our data. Requesting this via GET works all-right and returns the expected results. But sadly it doesn't works with POST.
To be precise, here is the request for the Capabilities with GET, that returns a valid GetCapabilities-Response:
http://myserver:8080/geoserver/wfs?service=wfs&version=1.1.0&request=GetCapabilities
I test this with wget, so the command looks like that:
wget -O wfs 'http://myserver:8080/geoserver/wfs?service=wfs&version=1.1.0&request=GetCapabilities'
Now I try the Capabilities-request with POST. I create a file with the request (named request) with the following content:
<GetCapabilities
service="WFS"
xmlns="http://www.opengis.net/wfs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs
http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"/>
This I run against the Geoserver with the following wget:
wget -O wfs --post-file=request 'http://myserver:8080/geoserver/wfs'
But now I get an OWS-Exception:
<ows:ExceptionReport xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.0" xsi:schemaLocation="http://www.opengis.net/ows/1.1 http://moa:8080/geoserver/schemas/ows/1.1.0/owsAll.xsd">
<ows:Exception exceptionCode="MissingParameterValue" locator="request">
<ows:ExceptionText>Could not determine geoserver request from http request org.geoserver.platform.AdvancedDispatchFilter$AdvancedDispatchHttpRequest#1e5c2cc</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
This looks like no POST-body has been sent or it was ignored. What do I wrong here?
EDIT: OK, I solved the problem. The problem is Geoserver expects a Content-Type-Header for Posting a XML-File. So correct request looks like the following:
wget -O wfs --header='Content-Type: text/xml' --post-file=request.xml 'http://myserver:8080/geoserver/wfs'
This returns the expected result.
I tried to investigate in your case but I don't have a server, so I used http://demo.opengeo.org/geoserver/web/
GET test: http://demo.opengeo.org/geoserver/wfs?service=wfs&version=1.1.0&request=GetCapabilities
I got a full response like you did.
POST test: I used http://www.hurl.it/ because I am on a Windows computer. With the following parameters:
URL: http://demo.opengeo.org/geoserver/wfs
Parameters: add body > same as yours:
<GetCapabilities
service="WFS"
xmlns="http://www.opengis.net/wfs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs
http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"/>
And I got the same response as the GET version.
Can you try the same tests with this demo server?
UPDATE
After a few comments chatting, the OP find himself the solution. The POST call was missing the Content-Type-Header information which is mandatory.

Strange Rails behavior with custom mime types

I am using Ruby on Rails 4.1 and I am trying to implement an API with a custom mime type. That is, in config/initializers/mime_types.rb I register an alias as like the following:
Mime::Type.register_alias 'application/json', :my_json
From another system I am trying to access the API with curl by running a HTTP PUT request, this way:
curl http://www.my_api.org/articles.my_json --request PUT --header "Content-Type: application/json" --data-binary '{\"key\": {\"a\": \"1\", \"b\": \"2\"}}'
However, by inspecting the ArticlesController parameters in my Rails application, I get the following output (note: article parameters are "unwanted" and those duplicate the "wanted" key parameters):
Parameters: {"key": { "a"=>"1", "b"=>"2" }, "article": { "a"=>"1", "b"=>"2" }}
What is the problem? Is it a bug? How can I solve that?
Note: I have implemented and access other similar API by executing HTTP GET requests and all works as expected. The problem seems to happens only when I execute HTTP PUT requests.
#rafaelfranca - No it is not a bug. It is how wrap_parameters works. You can disable at this file in your application config/initializers/wrap_parameters.rb.
See github.

Resources