Hi,

I'm consuming a REST API that only accepts x-www-form-urlencoded.

I'm trying to request an object:

id={

  "number":"500",

  "month":"12",

  "year":"20",

}


I understand: number=500&month=12&year=20; the API also requires 'id'.

How would the 'id' be included?


Thanks,

Brad



Solution

You might not need to send an id field at all, but if the API specifies that this is required, this StackOverflow describes how to correctly nest form parameters:


id[number]=500&id[month]=12&id[year]=20
Solution

Thank you. I tried but did not work.  OutSystems converted to 'text/plain'.

I also tried %5b/%5d; referenced in StackOverflow, this was also converted to 'text/plain'.

I confirmed, API requires the id field; the API will take different id fields for example id, accessid, tokenid, etc. and provide a response.


Any additional thoughts?

That sounds like an incorrect Content-Type in your API call.


Try adding a Content-Type Request Header as an input to your REST call, and set its value to the required type to avoid sending plaintext.

application/x-www-form-urlencoded

I added the content-type.

When running a test in API window; it works fine, but when running in web emulator and OutSystems Now I get an error (400 bad request).

In checking the API service; the 'card' has dropped from the post body. 

Thoughts?

Thanks!

Brad


That's very strange, but probably not related to your API implementation anymore.


Run the code through the built-in debugger and make you're passing all required inputs to the API call.


APIs are always invoked server-side (IIS, Wildfly, or other) so calling it from the web emulator and OutSystems Now shouldn't make a difference here.

Fixed it. Thank you for the help.

When using Content-Type input parameter; few things I did.

  • Location of the input parameter should match the location in post body. (Content-type was first under API Action)
  • Request format should be plain text.
  • Plain text has only 1 input parameter of text.
  • Using an Assign, Built the 'id' outside the API using the structure you mentioned above.
  • I don't believe the structure can be built with the API as there's only 1 input.   

I didn't realize the above.

Thank you again for the help and coaching! Much Appreciated!

Thanks,

Brad 


Great to hear!


A little late, but just discovered this guide as well: https://www.outsystems.com/forums/discussion/36156/guide-consume-rest-api-with-form-url-encoded-request/ 


Leaving it here for future reference.