TOPIC:

Using CURL POST to call API 3 weeks 3 days ago #277276

  • Topic Author
  • JD_Tampa
  • Offline
  • Fresh Breezer
  • Fresh Breezer
  • Posts: 4
  • Thanks: 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.

Please Log in or Create an account to join the conversation.

Using CURL POST to call API 3 weeks 3 days ago #277278

  • Topic Author
  • JD_Tampa
  • Offline
  • Fresh Breezer
  • Fresh Breezer
  • Posts: 4
  • Thanks: 0
FYI -
Joomla! 3.9.23
BreezingForms 1.9.1 Stable (build 941)

Please Log in or Create an account to join the conversation.

Using CURL POST to call API 3 weeks 3 days ago #277294

  • Mirec's Avatar
  • Mirec
  • Offline
  • Ultimate Breezer
  • Ultimate Breezer
  • Posts: 3158
  • Karma: 2
  • Thanks: 153
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

Please Log in or Create an account to join the conversation.

Using CURL POST to call API 3 weeks 3 days ago #277319

  • Topic Author
  • JD_Tampa
  • Offline
  • Fresh Breezer
  • Fresh Breezer
  • Posts: 4
  • Thanks: 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.

Please Log in or Create an account to join the conversation.

Using CURL POST to call API 3 weeks 3 days ago #277320

  • Mirec's Avatar
  • Mirec
  • Offline
  • Ultimate Breezer
  • Ultimate Breezer
  • Posts: 3158
  • Karma: 2
  • Thanks: 153
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

Please Log in or Create an account to join the conversation.

Last edit: Post by Mirec.

Using CURL POST to call API 3 weeks 3 days ago #277321

  • Topic Author
  • JD_Tampa
  • Offline
  • Fresh Breezer
  • Fresh Breezer
  • Posts: 4
  • Thanks: 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.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2
Moderators: ForumSupport
Time to create page: 0.051 seconds

BreezingForms Pro 1.4.7 for WordPress Released!

Available in the membership section.

September Discount!

Massive discounts on all subscriptions!

Get Your Subscription Here

Quick Links

Downloads

BreezingForms

ContentBuilder

BreezingCommerce

Templates

Documentation

BreezingForms

ContentBuilder

BreezingCommerce

Apprendre BreezingForms (French Community)

Apprendre et maîtriser BreezingForms par des tutoriels et exemples, le tout en français

breezingforms.eddy-vh.com

Questions et réponses sur les forums de l'AFUJ

AFUJ

Special Offer

Summer Sale! All subscriptions at a special price!

Includes prio support, all of our current and future Joomla!® extensions and Joomla!® templates for the duration of your membership.

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!