How to POST JSON data using ColdFusion 10 with Rest Services? - api

I am using ColdFusion 10. I have registered one rest service in ColdFusion Administrator by just adding path to my cfc and service mappings in the rest services tab. I am able to simply GET the data.
Similar to this I wanted a simple example to POST the JSON data but I am having issues doing that. I am using code below and trying to POST the data.
<cfcomponent rest="true" restpath="restService">
<cffunction name="postData" access="remote" returntype="any" produces="application/json" httpmethod="POST">
<cfsavecontent variable="JSONData">
{
"customer": {
"first_name": "Steve",
"last_name": "Lastnameson",
"email": "steve.lastnameson#example.com",
"phone": "+15142546011",
"verified_email": true,
"addresses": [
{
"address1": "123 Oak St",
"city": "Ottawa",
"province": "ON",
"phone": "555-1212",
"zip": "123 ABC",
"last_name": "Lastnameson",
"first_name": "Mother",
"country": "CA"
}
]
}
}
</cfsavecontent>
<cfhttp url="http://localhost:8080/rest/restApp/restService" method="post" timeout="300" result="httpResponsePosttest">
<cfhttpparam type="header" name="content-type" value="application/json" />
<cfhttpparam type="header" name="content-length" value="#Len(Trim(JSONData))#" />
<cfhttpparam type="header" name="charset" value="utf-8" />
<cfhttpparam type="body" value="#Trim(JSONData)#" />
</cfhttp>
<cfreturn httpResponsePosttest>
</cffunction>
When I tried to dump httpResponsePosttest I am getting this with blank error message and making it hard to debug.
When I tried to hit the same URL in postman with method as POST I am getting "The request has exceeded the allowable time limit tag" as a response.
Can somebody please help me out with this?

Related

ByBit : API ColdFusion

Having trouble with ByBit By/Sell API. ColdFusion any help appreciated.
https://bybit-exchange.github.io/docs/spot/v3/?console#t-authenticationparameters
Authentication for POST
POST
rule: timestamp + api_key + recv_window + raw_request_body
param_str =
"1659073093578T0d98KyVamQ62YBzN85000{
"symbol": "BTCUSDT",
"orderQty":"0.05",
"side": "Sell",
"orderType": "LIMITT",
"timeInForce": "GTC",
"orderPrice": "24500",
"orderLinkId": "spotA0008"
}"
curl --location --request POST 'https://api-testnet.bybit.com/spot/v3/private/order' \
--header 'X-BAPI-API-KEY: {api key}'
--header 'X-BAPI-TIMESTAMP: 1659067662307'
--header 'X-BAPI-RECV-WINDOW: 5000'
--header 'X-BAPI-SIGN: cc63fb44be4a87f4b7bbd42db012ddacc1c935c3d3ae3e01c3b4be393522c213'
--header 'Content-Type: application/json'
--data-raw '{
"symbol": "BTCUSDT",
"orderQty":"0.01",
"side": "Buy",
"orderType": "LIMIT",
"timeInForce": "GTC",
"orderPrice": "21300",
"orderLinkId": "spotx006",
"orderCategory": 1,
"triggerPrice": "21700"
}'
This is the Post Example.
For Account - my signature works fine.
<cfscript>
apiKey = "#_key#";
apiSecret = "#_s#";
newbody = serializeJSON({
"symbol": "#symb#",
"orderQty":"#qty#",
"side": "#side#",
"orderType": "#type#"
});
ts_key_str = #unixdatetimeNow.getTime()# & '#apikey#' & '5000';
str_to_sign = #unixdatetimeNow.getTime()# & '#apikey#' & '5000' & '#newbody#';
HMAC = hmac(str_to_sign, apiSecret, "HMACSHA256");
</cfscript>
<cfhttp url="#base_api##req_path#" method="POST" result="result" charset="utf-8">
<cfhttpparam type="body" value="#newbody#">
<cfhttpparam type="HEADER" name="Content_Type" value="application/json">
<cfhttpparam type="header" name="X-BAPI-SIGN-TYPE" value="2">
<cfhttpparam type="header" name="X-BAPI-API-KEY" value="#_key#">
<cfhttpparam type="header" name="X-BAPI-RECV-WINDOW" value="5000">
<cfhttpparam type="header" name="X-BAPI-SIGN" value="#lhmac#">
<cfhttpparam type="header" name="X-BAPI-TIMESTAMP" value="#unixdatetimeNow.getTime()#">
</cfhttp>
Even adding the ts_key_str in front of new body does not work either.
I get bad signature. When getting account data I using this it works fine cfhttpparam type="body" value=""
Any help appreciated.
It was a timestamp issue.
Set one timestamp, and use that timestamp. So it doesn't maybe pull different timestamp on a delayed call.
Updated code.
<!--- MARKET --->
<cfset b_body = '{"symbol": "#pair#","orderQty":"#size#","side": "#side#","orderType": "MARKET"}'>
<cfset unixdatetimeNow = dateConvert( "local2Utc", now() )>
<cfset timestamp = #unixdatetimeNow.getTime()#>
<cfscript>
apiKey = "#_key#";
apiSecret = "#_s#";
str_to_sign = #timestamp# & '#apikey#' & '5000' & '#b_body#';
HMAC = hmac(str_to_sign, apiSecret, "HMACSHA256");
</cfscript>
<cfset lhmac = lCase(#hmac#)>
<cfhttp url="#base_api##req_path#" method="POST" result="result" charset="utf-8">
<cfhttpparam type="body" value="#b_body#">
<cfhttpparam type="header" name="X-BAPI-SIGN-TYPE" value="2">
<cfhttpparam type="header" name="X-BAPI-SIGN" value="#lhmac#">
<cfhttpparam type="header" name="X-BAPI-API-KEY" value="#_key#">
<cfhttpparam type="header" name="X-BAPI-TIMESTAMP" value="#timestamp#">
<cfhttpparam type="header" name="X-BAPI-RECV-WINDOW" value="5000">
<cfhttpparam type="HEADER" name="Content_Type" value="application/json">
</cfhttp>

How to display the data of a Web Service in a RadzenDataGrid?

First of all, let me clarify that I am using Blazor and what I want is to display the data from a web service (XML) to my Blazor RadzenDataGrid component.
I found out how to do it with a database model, which is posed as follows:
<RadzenDataGrid id="tabla_trabajadoresSinUs" style="display: none" AllowFiltering="true" AllowColumnResize="true"
FilterMode="FilterMode.Simple" PageSize="5" AllowPaging="true" AllowSorting="true" Data="#personas" TItem="SisPersona" ColumnWidth="150px"
FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
LogicalFilterOperator="LogicalFilterOperator.Or">
<Columns>
<RadzenDataGridColumn TItem="SisPersona" Property="Id" Title="Id" Frozen="true" TextAlign="TextAlign.Center" Width="100px" />
<RadzenDataGridColumn TItem="SisPersona" Property="Nombre" Title="Nombre" TextAlign="TextAlign.Center" Width="100px"/>
<RadzenDataGridColumn TItem="SisPersona" Property="Nif" Filterable="false" TextAlign="TextAlign.Center" Title="Código Tarea" Width="100px" />
<RadzenDataGridColumn TItem="SisPersona" Property="Direccion" Filterable="false" Title="Usuario I." Width="100px" />
</Columns>
</RadzenDataGrid>
#code {
DataBaseContext db = new DataBaseContext();
IEnumerable<SisPersona> personas;
protected override void OnInitialized() {
personas = db.SisPersonas;
}
}
But this is not what I really want, I integrated my web services in my project and through a method of my web service (GetStaffMemebers()) that I have in a class I want it to display that information in my RadzenDataGrid, is there supposed to be a way to do this?
The bottom line is that I don't know how to adapt a web service in this Blazor component.
Help is appreciated as always!

How to enable CORS in Mulesoft on application level?

I need to enable CORS in Mulesoft on the application level. I already have found some solutions by setting CORS on the listener, but it doesn't work for me. Here is my HTTP listener configuration:
<http:listener-config name="erp-apoteka-api-httpListenerConfig">
<http:listener-connection host="0.0.0.0" port="8081" protocol="HTTPS"/>
<http:listener-interceptors>
<http:cors-interceptor>
<http:origins>
<http:origin url="http://localhost:3000" accessControlMaxAge="30000">
<http:allowed-methods>
<http:method methodName="GET" />
<http:method methodName="POST" />
<http:method methodName="DELETE" />
<http:method methodName="OPTIONS" />
<http:method methodName="PUT" />
</http:allowed-methods>
<http:allowed-headers >
<http:header headerName="Content-Type" />
<http:header headerName="Access-Control-Allow-Headers" />
<http:header headerName="Access-Control-Allow-Origin" />
</http:allowed-headers>
<http:expose-headers />
</http:origin>
</http:origins>
</http:cors-interceptor>
</http:listener-interceptors>
</http:listener-config>
I'm using React and Axios on frontend and when I do some calls, I got this error:
Access to XMLHttpRequest at 'http://localhost:8081/api/kategorije' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
KategorijeContainer.tsx:53 Error: Network Error
at createError (createError.js:16)
at XMLHttpRequest.handleError (xhr.js:83)
xhr.js:178 GET http://localhost:8081/api/kategorije net::ERR_FAILED
My axios settings are this:
export const axiosInstance = axios.create({
baseURL: BaseRoutes.ApiUrl,
headers: {
'Content-Type': 'application/json',
},
responseType: 'json',
});
Your http:listener-connection protocol is set to HTTPS but your origin is coming from http://localhost.. I'm assuming you're still in your development phase and have no TLS enabled on your mule backend, try changing it to HTTP.

Magento2 checkout form: How to display placeholder attribute value in fields?

Goodmorning Stackoverflow,
I'm customizing the checkout onepage in Magento 2. Now I'm trying to display placeholders instead of labels in the shipping-address form, but no success till now. I hope someone can help me out
Cheers, Jorge
UPDATE:
In the console i can see a variable is giving to the attribute placeholder of the element input.
<input class="input-text" type="text" data-bind="
value: value,
valueUpdate: 'keyup',
hasFocus: focused,
attr: {
name: inputName,
placeholder: placeholder, // <<<< right here
'aria-describedby': noticeId,
id: uid,
disabled: disabled
}" name="street[0]" placeholder="" aria-describedby="notice-BVWUCFN" id="BVWUCFN">
Now i would like to know if theres is a way to modify this variable via the backend, so i can display label name in the placeholder attr.
See screenshot
Apoligies for my bad english
Standard way,
Copy all html files from vendor/magento/module-ui/view/frontend/web/templates/form/element/
at
app/design/frontend/<Vendor>/<theme>/Magento_Ui/web/templates/form/element/
Then change all Change placeholder: placeholder to placeholder: label as mention by Akis Verillis.
Now you need to deploy the static files by below code:
php bin/magento setup:static-content:deploy
And see the magic.
Note: If you have checkout from github then try copy from
/app/code/Magento/Ui/view/base/web/templates/form/element/
Change placeholder: placeholder to placeholder: label
The answer is in Magneto 2 documentation now:
http://devdocs.magento.com/guides/v2.0/howdoi/checkout/checkout_edit_form.html
The templates are the ones mentioned in previous answers.
The templates from magento-ui module are used in other places than checkout.
In your custom module directory, create a new /view/frontend/layout/checkout_index_index.xml file. In this file, add content similar to the following:
...
<referenceBlock name="checkout.root">
<arguments>
<argument name="jsLayout" xsi:type="array">
...
<item name="shippingAddress" xsi:type="array">
<item name="children" xsi:type="array">
<!-- The name of the form the field belongs to -->
<item name="shipping-address-fieldset" xsi:type="array">
<item name="children" xsi:type="array">
<!-- the field you are customizing -->
<item name="telephone" xsi:type="array">
<item name="config" xsi:type="array">
<!-- Assigning a new template -->
<item name="elementTmpl" xsi:type="string">%Vendor_Module%/form/element/%your_template%</item>
%Vendor_Module%/form/element/%your_template%
path is [your theme dir]/Vendor_Module/web/template/form/element/your_template.html
Clear browser cache too beside:
Delete all files in the pub/static/frontend and var/view_preprocessing directories.
If this is useful for you, the definition of that element is in:
/app/code/Magento/Ui/view/base/web/templates/form/element/input.html
It defines an input as:
<input
class="admin__control-text"
type="text"
data-bind="
value: value,
hasFocus: focused,
attr: {
name: inputName,
placeholder: placeholder,
'aria-describedby': noticeId,
id: uid,
disabled: disabled
}" />
You can add placeholder item to a layout.xml file for your field. Just like this:
<item name="address" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/element/abstract</item>
<item name="config" xsi:type="array">
<item name="customScope" xsi:type="string">contactForm</item>
<item name="template" xsi:type="string">ui/form/field</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/input</item>
</item>
<item name="placeholder" xsi:type="string">Address</item>
<item name="dataScope" xsi:type="string">address</item>
<item name="label" xsi:type="string">Address</item>
<item name="sortOrder" xsi:type="string">20</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="string">true</item>
</item>

Using iText to save pdf to ColdFusion Variable

I need to use iText with ColdFusion (CF) because CFDocument won't do everything I need it to, however I would like to return the result into a CF Variable instead of saving it to a file. It seems that every example out there saves the results to a file.
I'm using the following example code to actually generate a pdf but as I said I need it in a variable (preferably without being a file first) because that variable has to be passed on to code another group has written (and I have no control over).
<cfset var document=createObject("java", "com.lowagie.text.Document") />
<cfset var PageSize = createObject("java","com.lowagie.text.Rectangle") />
<cfset var fileIO = createObject("java","java.io.FileOutputStream") />
<cfset var writer = createObject("java","com.lowagie.text.pdf.PdfWriter") />
<cfset var paragraph = createObject("java", "com.lowagie.text.Paragraph") />
<cfset var FontFactory = createObject("java","com.lowagie.text.FontFactory") />
<cfset var Font = createObject("java", "com.lowagie.text.Font") />
<cfset var Courier = Font.init(Font.COURIER, 8.0) />
<cfset var CourierB = Font.init(Font.COURIER_BOLD, 8.0) />
<cfset PageSize.init(612, 792) />
<cfset document.init(PageSize) />
<cfset fileIO.init("C:\test.pdf") />
<cfset writer.getInstance(document, fileIO) />
<cfset document.open() />
<cfset paragraph.init("Hello world.", Courier) />
<cfset document.add(paragraph) />
<cfset document.close() />
I'm not all that great with Java, just basic knowledge, so this could actually be something simple that I'm not understanding.
Thanks
Leigh's comment about using ByteArrayOutputStream was the correct answer.
Here is the updated code that works placing the generated PDF into a ColdFusion variable:
<cfset var document=createObject("java", "com.lowagie.text.Document") />
<cfset var PageSize=createObject("java","com.lowagie.text.Rectangle") />
<cfset var fileIO=createObject("java","java.io.ByteArrayOutputStream") />
<cfset var writer=createObject("java","com.lowagie.text.pdf.PdfWriter") />
<cfset var paragraph=createObject("java", "com.lowagie.text.Paragraph") />
<cfset var FontFactory=createObject("java","com.lowagie.text.FontFactory") />
<cfset var Font = createObject("java", "com.lowagie.text.Font") />
<cfset var Courier = Font.init(Font.COURIER, 8.0) />
<cfset PageSize.init(612, 792) />
<cfset document.init(PageSize) />
<cfset writer.getInstance(document, fileIO) />
<cfset document.open() />
<cfset paragraph.init("Hello world.", Courier) />
<cfset document.add(paragraph) />
<cfset document.close() />
<cfset var returnVar = fileIO.toByteArray() />