Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Call to php form from script

Pedro J. Acosta February 17, 2021

Hello everyone,

I need to make a call to a php form located in an external system, in the call I also need to pass a parameter to receive a 0 as a result, since if I pass the empty parameter it returns a 1, is it possible to perform this task from a script? , I have tried making a rest call but I always get the value 1.

form1.png

 

script1.png

 

Script:

import groovyx.net.http.RESTClient;
import groovyx.net.http.HttpResponseDecorator;
import groovyx.net.http.HttpResponseException;
import com.atlassian.jira.util.json.JSONException
import com.atlassian.jira.util.json.JSONObject
import static groovyx.net.http.ContentType.*;

def body_req = [
"texto": "hola"
]

RESTClient client = new RESTClient('http://tic.eroski.es/');
HttpResponseDecorator respPost = (HttpResponseDecorator) client.post(
path : '/codigo/tic_finan/tic_finan_test.php',
body : body_req,
requestContentType: JSON
)

return respPost.getData()

 

 

Regards.

1 answer

1 accepted

1 vote
Answer accepted
Danyal Iqbal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 17, 2021

look like your request body json is not correct.

Pedro J. Acosta February 17, 2021

Hello Danyal,

I am new to this kind of thing, any suggestions?

 

Regards.

Pedro J. Acosta February 18, 2021

Hello again Danyal,

If you have any idea how to fix the body of the json request so that it makes the call correctly it would be of great help.

 

Regards.

Danyal Iqbal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2021

Hi,

 the correct format should be availble in the REST API documentation. If you can't find it in the docs, try to get an existing record from the REST API with a GET request and see how the json is formatted.

I would investigate the REST Endpoint with Postman or any other rest client and make the POST Request work from Postman first.

some quick pointers:

- use a try catch block to catch the exception

- define the body directly directly when you make the call

- use respPost.responseData

- loose the quotation marks for Json Keys

- print the json string youo are posting to see how it look

- set the correct header:'application/json'

- check the status code ... respPost.statusCode. Is the api returning a 200?

def body_req = [
texto: "hola"
]

Like Pedro J. Acosta likes this
Pedro J. Acosta February 18, 2021

Hello Danyal,

I just got the script to work for me, the error was when defining the content-type, I have changed the value of the header to 'application / x-www-form-urlencoded' and it works correctly, thank you very much for your help.

 

Regards.

Suggest an answer

Log in or Sign up to answer