Solved

Syntax error when doing createCard using GuzzleHttp


Userlevel 4

Hello,

Could you please assist me understanding why I am getting a 400 Bad Request response when trying to use createCard using graphql?

Ex: 

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.pipefy.com/graphql', [
'body' => '{"query":"mutation { createCard( input: { pipe_id: '.$pipe_id.', phase_id: '.$phase_id.', title: "LOREM IPSUM", fields_attributes: [ { field_id: "title", field_value: "LOREM IPSUM1" }, { field_id: "email", field_value: "my-email@email.com" } ] }) { clientMutationId card { id title } } } ',
'headers' => $headers,
]);

Thanks!

icon

Best answer by genietim 13 May 2023, 17:00

View original

12 replies

Userlevel 4

Hello, @enrique.herbella. How are you?

So that we can better help you with your question, would it be possible for you to share the printout of your request and the response?
 

Something I noticed too, you are passing quotes in the pipe and phase ID parameters. We don't support quotes in number fields, only in string. 

Regards
Felipe

Userlevel 4

Hi @felipealves 

The apostrophes are there because $pipe_id and $phase_id are variables.
There are no apostrophes carried out to the actual request.

Here is a screenshot of the complete code I’m using so far:

 

You will find the Guzzle request debug file attached to this message (debug.txt).

The result of the request are as follows:

Thanks for your help!

Enrique

 

Userlevel 7

@felipealves can you help us on it, please?😀

Userlevel 4

@Lais Laudari. Sure, I can help.

@enrique.herbella, thank you for the information you sent, I'll take a look and get back to you as soon as I finish my analysis. 

Userlevel 4

Hello @enrique.herbella,

Sorry for the delay, analyzing the code you sent me. It seems to be because the pipe_id and phase_id are getting quotes, and because it is a number in GraphQL it needs to be unquoted, if it was a string then we would need to include quotes in it. 
 

Could you make this adjustment and validate it on your side?
 

<?php
$pipe_id = 303046137;
$phase_id = 318796214;
$debug = fopen(‘debug.txt’ ,  ‘a+’);

 

Userlevel 4

Hi @felipealves 
Unfortunatly I have made the change you have suggested but I’m still getting the same error. 

 

Could you please provide me with a working example of a createCard request?
Thanks!

Enrique
 

Userlevel 7
Badge +12

Hi @enrique.herbella 

It might be related to some issues with the pipe structure, some mandatory fields you do not supply in your request, or the phase not being set up with a phase form.

The following PHP code works perfectly fine in my test-pipe – feel free to use it as a basis, and try to experiment with what fields might be the reason for the issue you are seeing:

 

<?php

require_once("vendor/autoload.php");
use GraphQL\Client;

$pipe_id = 301464753;

$headers = [
'Authorization' => 'Bearer YourBearerHere'
];
$gqlClient = new Client('https://api.pipefy.com/graphql', $headers);
$query = <<<EOD
mutation {
createCard(input: {
pipe_id: $pipe_id,
fields_attributes: [
{ field_id: "test_field", field_value: "my-email@email.com" }
]
}) { clientMutationId card { id } }
}
EOD;
$response = $gqlClient->runRawQuery($query);
var_dump($response);

If I adjust it to submit a non-existant field, I get a `Fields not found` error. When I do not submit a mandatory field I get a `Invalid inputs` error.

 

Generally, it would be interesting to see the actual response you get, not just that it is a 400 error. You can wrap your `->request()` in try/catch, and use `->getResponse()` on the exception you catch, I guess.

Userlevel 4

Hi @genietim 

I have made more trials but unfortunatly I am not getting to get any other result than 400 bad request. 

I have tried to print ->getResponse() but it does not produce useful information. 

The pipe I am using has no fields configured. It has 3 phases however.
Here is my updated code. The result is attached to this message. Thank you for any help you can provide.
 

Enrique

 

Userlevel 7
Badge +12

Well, dear @enrique.herbella , without any fields configured, particularly not a field with ID “email” and ID “title”, at least your initial query will fail for sure.

Would you mind trying with my code above (which uses https://github.com/mghoneimy/php-graphql-client, which also uses GuzzleHTTP), replacing the pipe id with yours, just to eliminate the possibility that you have a typo or invalid API key or similar that I oversee? Also, I never go so unhelpful error messages using this library as the one you present, so maybe it could also help in that regard.

Userlevel 7
Badge +12

Are you familiar with Postman (https://www.postman.com/downloads/), @enrique.herbella ? It really helps to use it, just to see whether the code is the problem or the query/authentication. Also, it can readily output PHP code for you, including code that uses Guzzle.

Userlevel 7
Badge +12

After investing another 30 minutes trying to figure out what could be going wrong, I noticed that I can reproduce your error using explicitly your most recent query.

The reason are various typos in it, that I noticed now: you neighter close the “ opened for the mutation, nor the { opened before the query.

Actually, your query should therefore look something like this:

 

<?php

$pipe_id = 303046137;
$phase_id = 318796213;
$debug = fopen('debug.txt', 'a+');
require_once('vendor/autoload.php');
$headers = [
'accept' => 'application/json',
'authorization' => 'Bearer eyWhateverYoursIs',
'content-type' => 'application/json'
];
$client = new \GuzzleHttp\Client();
try {
$body = '{"query": "mutation {
createCard(input: {
pipe_id: ' . $pipe_id . ',
phase_id: ' . $phase_id . '
}) {
clientMutationId
card { id }
}
}"}';
$body = trim(str_replace("\n", "\\n", $body));
var_dump($body);
$response = $client->request('POST', 'https://api.pipefy.com/graphql', [
'body' => $body,
'headers' => $headers,
'debug' => $debug,
]);

var_dump($response);
} catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
var_dump($responseBodyAsString);
var_dump($e);
}

 

Userlevel 4

Hi @genietim 
Thank you for taking the time to assist me with this request.
I went back to the basics using the https://developers.pipefy.com/reference/graphql-endpoint tool and I have been able to fix my code. Thanks again!

Reply