I made a custom REST API on Magento 2. I want some params to be required and other to be no required or accept null. I have the next code:
Api/Order/ShippingInterface
/**
* API00004. Envío de Pedido
* Url Path: /rest/V1/orders/sap/shipping
* Se mandará a este servicio la información cuando un pedido ya ha
* sido enviado.
*
* @api
* @param string IdPedido
* @param string ControlNumber
* @param string UrlFile
* @param string DecodeFile
* @param string[] Trackings
* @return string JSON con Mensaje de Confirmación o Error.
*/
public function set(
$IdPedido = null,
$ControlNumber = null,
$UrlFile = null,
$DecodeFile = null,
$Trackings = null
);
Model/Api/Order/Shipping.php
/**
* API00004. Envío de Pedido
* Url Path: /rest/V1/orders/sap/shipping
* Se mandará a este servicio la información cuando un pedido ya ha
* sido enviado.
*
* @api
* @param string IdPedido
* @param string ControlNumber
* @param string UrlFile
* @param string DecodeFile
* @param string[] Trackings
* @return string JSON con Mensaje de Confirmación o Error.
*/
public function set(
$IdPedido = null,
$ControlNumber = null,
$UrlFile = null,
$DecodeFile = null,
$Trackings = null
){ ... }
But when I send a Json like:
{"IdPedido":null,"ControlNumber":null,"UrlFile":null,"DecodeFile":null,"Trackings":null}
It returns me the next error:
message":"One or more input exceptions have occurred.","errors":[{"message":"%fieldName is a required field.","parameters":{"fieldName":"IdPedido"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"ControlNumber"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"UrlFile"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"DecodeFile"}},{"message":"%fieldName is a required field.","parameters":{"fieldName":"Trackings"}}]
How can I do this?
Thanks.
-
Please post a code of your webapi.xml file.Aditya Shah– Aditya Shah2018年10月02日 04:50:56 +00:00Commented Oct 2, 2018 at 4:50
1 Answer 1
Open file
/vendor/magento/module-checkout/etc/webapi.xml
and inside try to locate this code
<route url="/V1/carts/mine/shipping-information" method="POST">
<service class="Magento\Checkout\Api\ShippingInformationManagementInterface" method="saveAddressInformation"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="cartId" force="true">%cart_id%</parameter>
</data>
</route>
You see, specifically inside the <data> tag, the <parameter> tag has attribute named force and it is equal to true in this case.
I'm inclined to say that this is the way do to it, in your case you will of course set force="false". Though, I have never tried this myself to be honest.
Anyway, open this link
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/service-contracts/service-to-web-service.html
and further click on Sample webapi.xml file
and I think you will find similar recommendations of XML handling this.
I hope this help, good luck.
-
This is not the answer of question, this process is used to override parameter values each time api gets hit.Kapil Dev Singh– Kapil Dev Singh2021年12月06日 08:37:30 +00:00Commented Dec 6, 2021 at 8:37