Http Connector Post Method With Content-Type application/json

I want to use data i have parsed form precious service and make post request for writing this data in another databasei in postman my post request looks like this :slight_smile:

and here is my http connector post request exmples :slight_smile:


and this request gives me this exception : Required request body is missing
How can i make post request so that my service could recognize request body?

Hi @Sally,

could you please share your bpmn process file.

Cheers
kristin

I have to evaluate json object , every time i try to start process on camunda form i got this error:
TypeError: undefined is not an Object in

Hi @Sally,

thank you, you can delete it.

Do you try to add the header Content-Type with value application/json and the body in your connector from type Text?
Is your Postman request working?

Cheers
kristin

@kristin thank you for your reply but I have added application/json in headers Contetn/Type and i have the same error , nothin changed

Did you mean that i shoud make my post request body by jss, to make it as an json object?

Hi @Sally,

I mean, you configured the body from Type Text and the value as JSON in the request of your screenshot above. The problem could be, that the JSON is not valid because the process variables e.g. cardNumber are not correct set.
You can try to use the connector configuration from your screenshot and instead of using the process variable values you could use hard coded values.
Of course, you can also use a script to build the body.

Cheers
kristin

1 Like

ok , thank you @kristin for you replies, but by the way i have several question about script implementation, i have generated script which should give me json request body but even this response isn’t accaptable for my post request body here is my jss code example, what should i cahnge to make my requst body accaptable for post request body?

var dat1=S(execution.getVariable("restResponse"));    
   var id = dat1.prop("id").value();
   var name = dat1.prop("Name").value();
      var data = new Object(); 
      data.id  = id;
      data.Name = name;
    
	  execution.setVariable("request body",data);

Hi @Sally,

please give me more details.

  • Do you have an error or stacktrace?
  • How looks your request/response configuration? Do you use the http connector?
  • Is the JS code snippet used for parsing the http response?

Cheers
kristin

Here is my service error:
1.

2018-02-01 03:37:23.799 WARN 9504 — [io-9001-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void ge.sda.springTest.Controller.DocumentController.addTopic(ge.sda.springTest.Data.DocumetsEntity)

  1. yes i use http connector
    in url i have this link http://localhost:9001/documents whih should make post request based on json data i will give it
    3.i have body parameter afeter headers parameter in which i have this script:

    var dat1=S(execution.getVariable(“restResponse”));
    var id = dat1.prop(“id”).value();
    var name = dat1.prop(“name”).value();
    var data = new Object();
    data.id = id;
    data.name = name;
    execution.setVariable(“request body”,data);

it should make json request body and send it to post request

PROCESS.bpmn (15.0 KB)

Hi @Sally,

I’m not sure whether I understand it right.

  • Do you want to execute a POST request to http://localhost:9001/documents?
  • Do you want to use as body with the created data object from the script? Or do you want to use the restResponse process variable as body? It looks that both contain the same content…

When you want to use the restResponse process variable as body, it should be enough to write in your script:
S(execution.getVariable('restResponse'));

Then you need to configure an output parameter in the http connector configuration. This output parameter contains the response of your post request.

Maybe, it could be helpful to have a look into the following example: https://github.com/camunda/camunda-bpm-examples/tree/master/servicetask/rest-service

Cheers
kristin

@kristin in my case restResponse’ is response from previous get request( i mean i make one service invokation to read data from it and then i want to get this data to my post request) when i try this
var data=S(execution.getVariable(“restResponse”));
execution.setVariable(“Request Body”,data);

i guess this doesn’t work inside my output parameter?
i want to use restResponse as a body for my post request

@Sally

I think, I understand it now…

First service invocation:

  • GET request: response is saved in restResponse

  • Is the response a JSON or which format?

  • Do you need the complete response or only parts of it?

  • Connector configuration, e.g.:

      <camunda:connector>
        <camunda:inputOutput>
          <camunda:inputParameter name="url">http://localhost:8181/ESeal</camunda:inputParameter>
          <camunda:inputParameter name="method">GET</camunda:inputParameter>
          <camunda:inputParameter name="headers">
            <camunda:map>
              <camunda:entry key="Accept">application/json</camunda:entry>
            </camunda:map>
          </camunda:inputParameter>
          <camunda:outputParameter name="restResponse">restResponse</camunda:outputParameter>
        </camunda:inputOutput>
        <camunda:connectorId>http-connector</camunda:connectorId>
      </camunda:connector>
    

Second service call:

  • Post request: body should be the content of restResponse

  • Connector configuration e.g.:

      <camunda:connector>
        <camunda:inputOutput>
          <camunda:inputParameter name="url">http://localhost:9001/documents</camunda:inputParameter>
          <camunda:inputParameter name="method">POST</camunda:inputParameter>
          <camunda:inputParameter name="headers">
            <camunda:map>
              <camunda:entry key="Accept">application/json</camunda:entry>
            </camunda:map>
          </camunda:inputParameter>
          <camunda:inputParameter name="body">
            <camunda:script scriptFormat="javascript"><![CDATA[S(execution.getVariable("restResponse"))]]></camunda:script>
          </camunda:inputParameter>
          <camunda:outputParameter name="postResponse">postResponse</camunda:outputParameter>
        </camunda:inputOutput>
        <camunda:connectorId>http-connector</camunda:connectorId>
      </camunda:connector>
    

Cheers
kristin

1 Like

@kristin you are right, according to your bnpm script example i shoudd write inside my input parameter ‘body’ something like this:
var dat1=S(execution.getVariable(“restResponse”));
execution.setVariable(“PostResponse”,dat1);

and then write in output parameter PostResponse and it will help me trasnform previous response data into post request body?

I tried this also but i got the same error