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

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.

Related

In Postman how to mock a post API call to return reponses based on the request body?

I'm trying to leverage Postman's mock server feature to mock an API that my application calls.
This is a Post request. I have gone through the documentation and as advised I have saved the responses as examples.
When I try hit the mock URL I get the postman error response
Here is my setup -
My Collection with saved examples
MY mock server
After going through your query, I can see that you're trying to match an example based on the body passed with the request.
To match an example based on the request body, you can leverage the body matching feature of mock servers by:
Enabling the body matching feature from the mock edit page (Reference: https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/setting-up-mock/#matching-request-body-and-headers).
OR
Passing an additional x-mock-match-request-body header with value as true along with your mock request to get the desired results.
You can find more information on how to use body matching feature with mock servers here: https://learning.postman.com/docs/designing-and-developing-your-api/mocking-data/matching-algorithm/#6-check-for-header-and-body-matching.
Do let me know if this doesn't solve your issue. In that case, it would be helpful if you can share the mock request that you're sending to get the response.

Using request data in Postman mock response

In my postman mock server I would like to use data from the request. Is this possible? I can't seem to find any reference to this scenario.
For example, my request includes a documentId value. I would like to capture that value and use it in the response.
Thanks.
Postman supports capture of URL path parameters for use in the response body, so e.g. if your example has https://my.example.com/v1/users/{{user_id}} in the URL, then you can use {{user_id}} in the response.
That's about as far as it goes though. You can't at present use data from query parameters, headers or the request body in your responses.
If you need to use other types of request data in your mock responses, you might want to check out MockLab. I've written up a detailed comparison of Postman mock servers and MockLab including a specific on dynamic responses and request data.

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.

Send SOAP Request with Django

First time using SOAP and wondering how can I make a simple SOAP request using django? I haven't yet tried setting up pysimplesoap, I first just want to make a connection to the webservice.
I have a string of the XML header and body
xml_header = ""
xml_body = ""
How can I send this request and wait for a response?
EDIT: I'm using Python 3.4 for SUDS is not an option
Another library worth checking out is Zeep.
Some of the more complex SOAP transactions are virtually impossible with SUDS, but are simple within ZEEP.
http://docs.python-zeep.org/en/master/
One way is to use the Suds library in your view: https://fedorahosted.org/suds/
Documentation: https://fedorahosted.org/suds/wiki/Documentation

Problem with Posting JSON object with WSRequest

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";