Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Internet
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 23-01-2020
XusF XusF is offline
Registrado
 
Registrado: abr 2019
Posts: 2
Poder: 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
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Mandar request a web Furyxe Internet 2 20-12-2012 04:28:48
delphi5 TIdHTTP problemas para enviar variables post con acentos ing_alsaac Internet 0 01-02-2011 21:11:36
invalid request BLR TJose Firebird e Interbase 3 16-08-2008 16:15:24
problemas con dataset.post barracuda Tablas planas 0 05-05-2006 22:13:08
IBDatase Insert Post Edit Post ariefez Firebird e Interbase 4 12-10-2005 01:53:04


La franja horaria es GMT +2. Ahora son las 00:09:04.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi