SOLVED: Using a JSON payload for a POST request

1
EDIT: SOLVED! Big thanks to @Eric and @Mike for the help. FOR ANYBODY TRYING TO USE A JSON REQUEST FOR A POST CALL: Make sure you escape ONLY the opening braces to a double brace ( i.e { → {{)   in the HTTP Headers tab you have to add a Custom HTTP header with Key “Content-Type” and Value “application/json” Working! Hi all, I was wondering if there is anyway to use a JSON payload in a POST request in any way other than export mapping (i.e can I just paste the request body somewhere?) When i attempt to paste it in the “request template” and escape the braces my request fails. {{     "owner":"111231",     "rEntity":[     ],     "rParty":[         {{             "role":"User",             "id":"123921",             "name":"TestUser"         }}     ],     "rPlace":[     ],     "desc":"brief description",     "cSpec":{{         "id":"Catalog"     }},     "attrs":[         {{             "value":"123921",             "name":"contact"         }},         {{             "value":"1",             "name":"location"         }}     ],     "requestedCompletionDate":"2020-10-01T15:35:06.163Z",     "externalId":null,     "createdBy":"admin" }}   here is the stack trace: An error has occurred while handling the request. [User 'Anonymous_b19a0727-706a-4499-a847-a19b6aaa4d8e' with session id 'cccf6f81-XXXX-XXXX-XXXX-XXXXXXXX41c6' and roles 'Administrator'] -------- com.mendix.modules.microflowengine.MicroflowException: Error calling REST service     at Module.Create_Cart_Rest (CallRest : 'Call REST (POST)') Advanced stacktrace:     at com.mendix.integration.actions.microflow.RestCallAction.execute(RestCallAction.scala:59) Caused by: com.mendix.modules.microflowengine.MicroflowException: 400: Bad Request     at Module.Create_Cart_Rest (CallRest : 'Call REST (POST)') Advanced stacktrace:     at com.mendix.integration.actions.microflow.RestCallAction.checkStatusCode(RestCallAction.scala:97)     at com.mendix.integration.actions.microflow.RestCallAction.handleResponse(RestCallAction.scala:79)     at com.mendix.integration.actions.microflow.RestCallAction.$anonfun$execute$2(RestCallAction.scala:55)     at com.mendix.integration.util.AutoCloseableUtil$.using(AutoCloseableUtil.scala:10)     at com.mendix.integration.actions.microflow.RestCallAction.execute(RestCallAction.scala:54)     at com.mendix.modules.microflowengine.microflow.impl.MicroflowObject.execute(MicroflowObject.java:49)     at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAfterBreakingIfNecessary(MicroflowImpl.java:215)     at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAction(MicroflowImpl.java:165)     at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:46)     at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.doCall(CoreActionHandlerImpl.scala:71)     at com.mendix.basis.actionmanagement.CoreActionHandlerImpl.call(CoreActionHandlerImpl.scala:49)     at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:54)     at com.mendix.basis.actionmanagement.ActionManagerBase$1.execute(ActionManagerBase.java:147)     at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32)     at com.mendix.basis.actionmanagement.ActionManagerBase.executeSync(ActionManagerBase.java:152)     at com.mendix.basis.actionmanagement.MicroflowCallBuilderImpl.execute(MicroflowCallBuilderImpl.scala:40)     at com.mendix.webui.actions.client.ExecuteMicroflowAction.runMicroflow(ExecuteMicroflowAction.scala:76)     at com.mendix.webui.actions.client.ExecuteMicroflowAction.$anonfun$apply$3(ExecuteMicroflowAction.scala:42)     at com.mendix.webui.actions.client.RegularClientAction$Helpers$.$anonfun$liftEither$1(RegularClientAction.scala:29)     at com.mendix.webui.actions.client.RegularClientAction$Helpers$StateHandler.$anonfun$apply$4(RegularClientAction.scala:55)     at com.mendix.webui.requesthandling.helpers.StateHandling.withState(StateHandling.scala:35)     at com.mendix.webui.requesthandling.helpers.StateHandling.withState$(StateHandling.scala:32)     at com.mendix.webui.actions.client.RegularClientAction$Helpers$StateHandler.withState(RegularClientAction.scala:45)     at com.mendix.webui.actions.client.RegularClientAction$Helpers$StateHandler.apply(RegularClientAction.scala:53)     at com.mendix.webui.actions.client.RegularClientAction$Helpers$StateHandler.apply(RegularClientAction.scala:45)     at com.mendix.webui.actions.client.ExecuteMicroflowAction.apply(ExecuteMicroflowAction.scala:43)     at com.mendix.webui.actions.client.ExecuteMicroflowAction.apply(ExecuteMicroflowAction.scala:22)     at com.mendix.webui.actions.client.RegularClientAction.$anonfun$execute$3(RegularClientAction.scala:109)     at scala.util.Try$.apply(Try.scala:213)     at com.mendix.webui.actions.client.RegularClientAction.$anonfun$execute$2(RegularClientAction.scala:109)     at com.mendix.webui.actions.client.RegularClientAction.$anonfun$execute$2$adapted(RegularClientAction.scala:107)     at com.mendix.webui.requesthandling.helpers.ContextHandling.$anonfun$inContext$5(ContextHandling.scala:47)     at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)     at com.mendix.basis.actionmanagement.IMonitoredAction$$anon$1.execute(IMonitoredAction.scala:47)     at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32)     at com.mendix.basis.actionmanagement.IMonitoredAction.monitor(IMonitoredAction.scala:49)     at com.mendix.basis.actionmanagement.IMonitoredAction.monitor$(IMonitoredAction.scala:25)     at com.mendix.webui.requesthandling.helpers.ContextHandling$ClientMonitoredAction.monitor(ContextHandling.scala:59)     at com.mendix.webui.requesthandling.helpers.ContextHandling.inContext(ContextHandling.scala:47)     at com.mendix.webui.requesthandling.helpers.ContextHandling.inContext$(ContextHandling.scala:27)     at com.mendix.webui.actions.client.RegularClientAction.inContext(RegularClientAction.scala:86)     at com.mendix.webui.requesthandling.helpers.ContextHandling.inContext(ContextHandling.scala:24)     at com.mendix.webui.requesthandling.helpers.ContextHandling.inContext$(ContextHandling.scala:19)     at com.mendix.webui.actions.client.RegularClientAction.inContext(RegularClientAction.scala:86)     at com.mendix.webui.actions.client.RegularClientAction.$anonfun$execute$1(RegularClientAction.scala:107)     at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)     at com.mendix.webui.requesthandling.helpers.ProfileHandling.profileRequest(ProfileHandling.scala:14)     at com.mendix.webui.requesthandling.helpers.ProfileHandling.profileRequest$(ProfileHandling.scala:10)     at com.mendix.webui.actions.client.RegularClientAction.profileRequest(RegularClientAction.scala:86)     at com.mendix.webui.actions.client.RegularClientAction.execute(RegularClientAction.scala:104)     at com.mendix.webui.requesthandling.ClientRequestHandler.handleAction(ClientRequestHandler.scala:104)     at com.mendix.webui.requesthandling.ClientRequestHandler.processRequest(ClientRequestHandler.scala:81)     at com.mendix.externalinterface.connector.RequestHandler.doProcessRequest(RequestHandler.java:40)     at com.mendix.external.connector.MxRuntimeConnector$1.execute(MxRuntimeConnector.java:76)     at com.mendix.external.connector.MxRuntimeConnector$1.execute(MxRuntimeConnector.java:73)     at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32)     at com.mendix.external.connector.MxRuntimeConnector.processRequest(MxRuntimeConnector.java:79)     at com.mendix.basis.impl.MxRuntimeImplBase.processRequest(MxRuntimeImplBase.java:930)     at com.mendix.m2ee.appcontainer.server.handler.RuntimeHandler.service(RuntimeHandler.java:42)     at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)     at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:755)     at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:547)     at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)     at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1610)     at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233)     at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1300)     at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188)     at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:485)     at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1580)     at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186)     at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1215)     at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)     at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127)     at org.eclipse.jetty.server.Server.handle(Server.java:500)     at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:383)     at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:547)     at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:375)     at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:273)     at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311)     at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)     at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117)     at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336)     at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313)     at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171)     at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129)     at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:375)     at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:806)     at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:938)     at java.base/java.lang.Thread.run(Thread.java:834) Thanks in advanced! 
asked
2 answers
3

To build on Mike’s point, I think you added {{ and }} to escape the braces in the Request body, when, in fact, you only need to escape the open { character.

There are 2 ways so solve this:

Option A:

  • Create a variable and put standard JSON in that variable as a string
  • Use that variable in the request body

Option B:

  • Leave your JSON in the request body ‘string template’ field
  • Escape only the open brace character with double braces, but leave the close brace as a single

 

answered
2

Arshia,

Not sure what the source of this JSON is, but its not valid JSON.  Here is what you included above:

And this is how it should look to be valid JSON:

{
	"owner": "111231",
	"rEntity": [],
	"rParty": [
		{
			"role": "User",
			"id": "123921",
			"name": "TestUser"
		}
	],
	"rPlace": [],
	"desc": "brief description",
	"cSpec": {
		"id": "Catalog"
	},
	"attrs": [
		{
			"value": "123921",
			"name": "contact"
		},
		{
			"value": "1",
			"name": "location"
		}
	],
	"requestedCompletionDate": "2020-10-01T15:35:06.163Z",
	"externalId": null,
	"createdBy": "admin"
}

in the first copy, I highlighted what was removed to make the JSON valid.  Without these brackets, you can use the JSON in a Mendix JSON structure (and an export mapping).

Maybe that will help you figure out how to get this working.

Mike

answered