Errore Api Magento 2 '% fieldName è un campo obbligatorio'?


9

Non sto ottenendo il risultato (id) mentre utilizzo il metodo POST usando url is rest / V1 / hello / test / 3

Screenshot :inserisci qui la descrizione dell'immagine

Ho seguito questo link per riferimento Fare clic qui

1) webapi.xml

<?xml version="1.0"?>
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
        <route url="/V1/hello/name/:name" method="GET">
            <service class="Inchoo\Hello\Api\HelloInterface" method="name"/>
            <resources>
                <resource ref="anonymous"/>
            </resources>
        </route>
        <route url="/V1/hello/test/:test" method="POST">
            <service class="Inchoo\Hello\Api\TestInterface" method="test"/>
            <resources>
                <resource ref="anonymous"/>
            </resources>
        </route>
    </routes>

2) TestInterface.php

<?php
namespace Inchoo\Hello\Api;

interface TestInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param id $name Users id.
     * @return id Greeting message with users id.
     */
    public function test($id);
}

3) Test.php

<?php
namespace Inchoo\Hello\Model;
use Inchoo\Hello\Api\TestInterface;

class Test implements TestInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param string $name Users name.
     * @return string Greeting message with users name.
     */
    public function test($id) {
        return "Hello How are you your id is:," .$id;
    }
}

4) di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Inchoo\Hello\Api\HelloInterface" type="Inchoo\Hello\Model\Hello" />
    <preference for="Inchoo\Hello\Api\TestInterface" type="Inchoo\Hello\Model\Test" />
</config>

Ora ho cancellato cache e pagecache e ho aperto l'app postmen e ho mantenuto l'URL come http://10.0.0.33/nagarajuM2/rest/V1/hello/test/3

ma sto ricevendo un errore.

Per favore aiutatemi.


Stai inviando una richiesta POST e in che cosa stai inviando?
— Emipro Technologies Pvt. Ltd. il

In realtà voglio stampare il valore ID usando il metodo post, quindi ho usato lo stesso concetto che abbia mai seguito inchoo. e nell'url sto usando come resto / V1 / ciao / nome / 4
— Nagaraju K

1
Per stampare prima devi inviare i dati POST come json: {"id": {}}
— Emipro Technologies Pvt. Ltd. il

ok fammi provare ..
— Nagaraju K

Grazie alle tecnologie @Emipro ora ho ottenuto il risultato. grazie per l'aiuto. prntscr.com/g0d53x
— Nagaraju K

Risposte:


6

Stai inviando una richiesta POST, quindi devi anche inviare i dati in una richiesta POST in formato json come:{ "id": {}}

E devo impostare Content-Type:application/jsonnell'intestazione http.


Oltre a menzionare "Content-Type: application / json" nella sezione prntscr.com/g0d96z
— Nagaraju K

6

Aggiorna correttamente i commenti della funzione sia per il file di interfaccia che per la classe che lo implementano.

interface TestInterface
{
    /**
     * Returns greeting message to user
     *
     * @param int $name
     * @return mixed
     */
    public function test($id);
}

0

Stavo ricevendo gli stessi problemi, poi ho notato che stavo usando POST invece di GET , modificandolo i problemi sono stati risolti.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.