Bienvenido, Invitado
Recordarme

TEMA:

Using CURL POST to call API 2 meses 2 semanas antes #277276

  • Autor del tema
  • JD_Tampa
  • Fuera de línea
  • Fresh Breezer
  • Fresh Breezer
  • Mensajes: 9
  • Gracias recibidas: 0
Greetings,
I have a need to call a REST API through my form processing (I'm using "Pieces"). When I run the code via command line or through straight Apache, the API returns good (successful) results. However when I use the same code through BF, I get a "bad request" every time.

I have tested various ways (including simplifying the JSON payload as much as possible), but it still fails (and still produces good results via Apache or command line). I have tried URLEncoding the JSON keys and values, escaping special characters, enclosing the data in single quotes, enclosing the data in double quotes, but still not able to "see" what the bad data is.

The code (I replaced the priority information) is below. I'm at a loss on what is happening in BF that changes the payload data ($createjson) to return a bad request.


<?php

$createurl = "http://my_url/my_api_call";
$createcontent = "Content-Type: application/json";
$createauth = "Authorization: ABCXYZ";

$jsonarray = array(
"Submitter" => "my_id",
"JSON_KEY1" => "JSON_VALUE1",
"JSON_KEY2" => "JSON_VALUE2",
"JSON_KEY3" => "JSON_VALUE3"
);

$createjson = '{"values": ' . json_encode($jsonarray) . "}";

$ch = curl_init();

curl_setopt_array($ch, array(
CURLOPT_URL => $createurl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $createjson,
CURLOPT_HTTPHEADER => array(
$createcontent,
$createauth
),
));

curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$headers)
{
global $locationurl;
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
if ($header[0] == "Location")
{
$locationurl = $header[1];
}
return $len;
}
);

$response = curl_exec($ch);
curl_close($ch);

echo "Response: " . $response ;

?>


Please help! I've spent 15 hours on this already.

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Using CURL POST to call API 2 meses 2 semanas antes #277278

  • Autor del tema
  • JD_Tampa
  • Fuera de línea
  • Fresh Breezer
  • Fresh Breezer
  • Mensajes: 9
  • Gracias recibidas: 0
FYI -
Joomla! 3.9.23
BreezingForms 1.9.1 Stable (build 941)

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Using CURL POST to call API 2 meses 2 semanas antes #277294

  • Avatar de Mirec
  • Mirec
  • Fuera de línea
  • Ultimate Breezer
  • Ultimate Breezer
  • Mensajes: 3265
  • Karma: 2
  • Gracias recibidas: 160
Hi,

you want sand data to another API, oke, could you please give me a little more info about API where you are sending it, do you have some link of API or something else where I can find useful info about API, so we can see which type of data API can receive and other things.

Let me know!

Regards,
Mirko

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Using CURL POST to call API 2 meses 2 semanas antes #277319

  • Autor del tema
  • JD_Tampa
  • Fuera de línea
  • Fresh Breezer
  • Fresh Breezer
  • Mensajes: 9
  • Gracias recibidas: 0
The API is for a product called Remedy (standard ticket tracking system).
I have removed the sensitive data, but this is the exact code that executes successfully from command line and also executes successfully from calling through Apache.

The only error I get when going through BF is :
[11-Sep-2022 20:37:12 UTC] PHP Notice: Undefined variable: locationurl in /var/www/html/components/com_breezingforms/facileforms.process.php(1246) : eval()'d code



<?php

$token = "valid_security_token";
$createurl = "http://my_url:8008/api/arsys/v1/entry/RKM:ProblemSolutionTemplate";
$createcontent = "Content-Type: application/json";
$createauth = "Authorization: AR-JWT ". $token;

$jsonarray = array(
"Submitter" => "Ego eimi",
"RKMTemplateTechnicianNotes" => "More notes for the Engineer",
"RKMTemplateProblem" => "Description of problem here",
"RKMTemplateSolution" => "Description of solution here"
);

$createjson = '{"values": ' . json_encode($jsonarray) . "}";

$ch = curl_init();

curl_setopt_array($ch, array(
CURLOPT_URL => $createurl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $createjson,
CURLOPT_HTTPHEADER => array(
$createcontent,
$createauth
),
));

curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$headers)
{
global $locationurl;
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
if ($header[0] == "Location")
{
$locationurl = $header[1];
}
return $len;
}
);

$response = curl_exec($ch);

echo "Response: " . $response . " ";
echo "Location: " . $locationurl;

curl_close($ch);

?>


Note - obviously the code "piece" in BF does not have the <?php and ?> tags.

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Using CURL POST to call API 2 meses 2 semanas antes #277320

  • Avatar de Mirec
  • Mirec
  • Fuera de línea
  • Ultimate Breezer
  • Ultimate Breezer
  • Mensajes: 3265
  • Karma: 2
  • Gracias recibidas: 160
Hi,

Note - obviously the code "piece" in BF does not have the <?php and ?> tags.

it's not necessary for that pieces! :)


Could you please send me the backend link and access data, so I will connect to your server and see what happens there, and what we can do, please send data by email ma@evolutio.hr

Please give me a little time because I'm mega busy!

If you have more questions, let me know!

Let me know when you sent the data!

Regards,
Mirko

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

Última Edición: Post by Mirec.

Using CURL POST to call API 2 meses 2 semanas antes #277321

  • Autor del tema
  • JD_Tampa
  • Fuera de línea
  • Fresh Breezer
  • Fresh Breezer
  • Mensajes: 9
  • Gracias recibidas: 0
This is not a public site, it's on our intranet; therefore I cannot send you any type of link where you can access.

Por favor, Identificarse o Crear cuenta para unirse a la conversación.

  • Página:
  • 1
  • 2
Moderadores: ForumSupport
Tiempo de carga de la página: 0.057 segundos