Ver Mensaje Individual
  #1  
Antiguo 23-01-2020
XusF XusF is offline
Registrado
 
Registrado: abr 2019
Posts: 2
Reputación: 0
XusF Va por buen camino
Problemas con Request POST a Woocommerce

Buenos días,

Estoy intentando hacer una petición REST a Woocommerce, concretamente intentando actualizar stocks de la tienda. La conexión con la API la he conseguido hacer, de hecho hago peticiones GET sin problema, pero al hacer un POST siempre me da un error:

Código:
'{"code":"rest_invalid_json","message":"Ha pasado un cuerpo JSON no v\u00e1lido.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}'
La autenticación es OAuth1. Estoy probando con un JSON muy sencillo que es el siguiente:

Código:
{
    "update": [
        {
            "id": 11563,
            "stock_quantity": 20
        }
    ]
}
Os enseño el código que estoy utilizando:

Código:
procedure TdmoDataModule.ActualizarStockWordpress;
var
  body: TJSONObject;
  updates: TJSONArray;
  product: TJSONObject;
  value: TJSONValue;
  s: string;
  lparam: TRESTRequestParameter;
  i: integer;

//  param: TJSONRequestParameter;

begin
  qryArticulos.Close;
  qryArticulos.Open;
  qryArticulos.First;

  body := TJSONObject.Create;
  updates := TJSONArray.Create;

  try
    while not qryArticulos.Eof do
    begin
      product := TJSONObject.Create;

      product.AddPair('id', TJSONNumber.Create(qryArticulos.FieldByName('CODIGO').AsInteger));
      product.AddPair('stock_quantity', TJSONNumber.Create(qryArticulos.FieldByName('STOCK_ACTUAL').AsInteger));
      updates.AddElement(product);

      qryArticulos.Next;
    end;

    body.AddPair(TJSONPair.Create('update', updates));

    showMessage(body.ToString);

    with dmoREST do
    begin
      RESTRequestProductsBatch.Params.Clear;

      RESTRequestProductsBatch.AddParameter(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_KEY_VALUE);
      RESTRequestProductsBatch.AddParameter(OAUTH_SIGNATURE_METHOD, OAUTH_SIGNATURE_METHOD_VALUE);
      RESTRequestProductsBatch.AddParameter(OAUTH_NONCE, OAuth1Authenticator1.nonce);
      RESTRequestProductsBatch.AddParameter(OAUTH_TIMESTAMP, OAuth1Authenticator1.timeStamp.DeQuotedString);
      RESTRequestProductsBatch.AddParameter(OAUTH_VERSION, OAUTH_VERSION_VALUE);
      s := OAuth1Authenticator1.SigningClass.BuildSignature(RESTRequestProductsBatch, OAuth1Authenticator1);
      RESTRequestProductsBatch.AddParameter(OAUTH_SIGNATURE, s);

      RESTRequestProductsBatch.AddBody(body);

      RESTRequestProductsBatch.Execute;
      showMessage(RESTResponseProductsBatch.Content);
    end;

  finally
    body.Free;
  end;
end;

Para hacer las peticiones GET tengo que añadir todos los parámetros que veis arriba por código, sino no funciona. Aquí he hecho lo mismo, y de hecho el error que me da no es de autenticación, sino que para ser que el error está en el JSON o en alguna propiedad del parámetro "body". He probado muchas combinaciones pero ninguna me ha dado resultado.

A ver si alguien es capaz de echarme una mano.

Muy agradecido,

Xus
Responder Con Cita