Problem with Posting JSON object with WSRequest - web-services

I want Play to call a webservice. The webservice accepts application/json and returns this as well. With the following code I'm trying to achieve this. (Note, the headers.put(xxx) are added later in an effort to solve the problem).
WSRequest request = WS.url(targetURL);
request.body = new Gson().toJson(user);
request.headers.put("Content-type","application/json");
request.headers.put("Accept","application/json");
request.post();
The strange thing is that my JBOSS server replies: "Cannot consume content type". If I use my 'Simple REST client' plugin in my Chrome browser, and provide the entire JSON Body GSon created and add the content-type header, I get a valid response. Is this not the way to send JSON to the server? Or am I missing some fundamental piece here?

While checking the API documentation on the WSRequest class i noticed the field mime-type
By setting it as follows JBOSS (resteasy) accepted my request succesfully.
request.mimeType = "application/json";

Related

How to read request's body in WSO2 ESB?

Hi everyone I am new using WSO2 ESB, here is the issue I made a integration project to learn but I have not been able to read the body of my request when I try I see this warning "Json Payload is empty"
enter image description here
Here is how I am trying to read the body <filter regex="[1-9]" source="json-eval($.test)">
enter image description here
And finally here is my request, I already set the header application/json
enter image description here
If anyone could help me, I would be very grateful. Thanks in advance
The request needs to be a POST to see the payload hit the ESB.
With GET it will just do a GET request without it.
Technically you can do GET with body but it's not a very common scenario.[1]
[1]https://docs.wso2.com/display/EI611/Unusual+Scenarios+for+HTTP+Methods+in+REST#UnusualScenariosforHTTPMethodsinREST-UsingGETwithaBody
That is because the GET method in wso2 does not allow the body payload. You can read in wso2 documentation:
Typically, a GET request does not contain a body, and the ESB profile does not support these types of requests. When it receives a GET request that contains a body, it drops the message body (...)
From doc: Using GET with a Body
Additional it is not good practice to do that, refer to this question: HTTP GET with request body
So you should use POST request.

Creating asset on Bigchaindb server using http call

I have created an account on bigchaindb site. Now I want to post some data to online server using http call by postman. I got it that I need to mention api_key and app_id in header. What I need to keep in body and what other parameters should be passed ?
I'm not sure what all you need to tell Postman, but here's a start:
method = POST
URL = https://test.bigchainb.com/api/v1/transactions?mode=commit
Headers
app_id: value
app_key: value
Content-Type: application/json
Body
The final, signed (fulfilled) transaction goes in the body, but I'm not sure what format Postman expects it in. Maybe a Unicode JSON string?
To construct a valid signed transaction, you should probably use one of the BigchainDB drivers, and if you're doing that, then why not also use the same driver to POST the transaction to the BigchainDB Testnet? Here's a list of drivers:
http://docs.bigchaindb.com/projects/server/en/master/drivers-clients/index.html

Issue connecting SOAP through Postman

I’m stuck with trying to connect to my SOAP API. The goal is to retrieve a quote via the “Getquote” function which is available in our webservice and use that quote in an application in Bubble.is. Therefore, I want to make it work through form-data so I can reuse the keys and values in Bubble. I get a succesfull quote through the raw method. See picture
Raw method:
You can see that all my fields are in the body so with the form-data method I put all the individual fields in key and value but I get the error message you see below.
Form data method:
Can someone see what I'm doing wrong? Excuses me for I am just starting. There might be some beginner mistakes in there. Thanks for the help!
SOAP encodes messages by using XML. Form data uses a completely different encoding, which the SOAP server doesn't understand, hence the error.
Although I've never used it, there is a Chrome extension called Boomerang that supports SOAP requests, and which may suit you better.

WSO2 API Manager "error": "no response from server"

I have published API in WSO2 using swagger JSON. After publish I am trying to call rest api using swagger in APP Console. It says
Response Body no content
Response Code 0
Response Headers {
"error": "no response from server"
}
There is no any error on server which will help me to understand problem.
Here is the request URL which I am using in local server : https://192.168.1.118:8243/api/2.0/questions/1/answers?start=1&end=1&fields=answerId%2CanswerDescription%2CcreateDate
In my API there are some custom header parameter. Because of this custom header parameter it was not working. I have added custom header parameter in api-manager.xml file.
<Access-Control-Allow-Headers>authorization,Access-Control-Allow-Origin,Content-Type,loggedInUserId,accessToken,clientToken</Access-Control-Allow-Headers>
I have faced a similar issue. I have edited the URL(from apicreator login) after it was published for first time and published it again. But the changes were never reflected. WSO2 was pointing to the old URL. Check the logs for error at "WSO2 HOME\repository\logs\wso2-apigw-errors" . Create another version and publish again and it should work.
The error is real, but misleading. In most likelihood, you've set the spec so that it returns a specific content type (say, application/json) but you actually return plain text (like a string or a number). swagger-ui expects it to be a JSON, tries to parse it and fails, giving you the wrong error message. However, it does mean your spec does not match what your API actually does.

How to http post with an empty body request using WS API in Playframework 2 / Scala?

I try to send an HTTP POST request to a service endpoint using Play2/Scala WS API. Since there is no parameters to be sent in the HTTP POST body, how can I send it using
WS.url("http://service/endpoint").post()
I have tried post() without argument but it gave me an error.
Cannot write an instance of Unit to HTTP response. Try to define a
Writeable[Unit]
Can you please help on this ?
thanks in advance...
Since post awaits a value that implements the Writeable and ContentTypeOf type classes,
you can use the Results.EmptyContent from play.api.mvc. (See API)
So I guess
WS.url("http://service/endpoint").post(Results.EmptyContent())
should do. (Didnt test)
For Play 2.6 and after, you have to use play.api.libs.ws.EmptyBody.
import play.api.libs.ws.{EmptyBody, WSClient}
WS.url("http://service/endpoint").post(EmptyBody)
Typical error is:
Cannot find an instance of play.api.mvc.Results.EmptyContent to WSBody. Define a BodyWritable[play.api.mvc.Results.EmptyContent] or extend play.api.libs.ws.ahc.DefaultBodyWritables
As of Play 2.8, you cannot use the WSRequest.post(body) methods with an empty body, because the BodyWritable trait requires a non-empty Content-Type
Instead, you can do ws.url(u).execute("POST") to send an HTTP POST request with no body.