1

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.

asked Oct 1, 2018 at 18:02
1
  • Please post a code of your webapi.xml file. Commented Oct 2, 2018 at 4:50

1 Answer 1

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.

answered Oct 1, 2018 at 19:13
1
  • This is not the answer of question, this process is used to override parameter values each time api gets hit. Commented Dec 6, 2021 at 8:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.