Incorrect Response Data to API Call

Hi,

I have a simple php script on my server that pulls data from a MySQL database.

I can run the script in Postman and in my browser and I get back data -

But when I run the Thunkable script -

I get this -

Any assistance would be appreciated.

Thanks
Steve

Anyone ?? @tatiang @matt_conroy

I always use API endpoints so I’ve never had to work with a php script for that. I think it’s possible based on searching the forums but I don’t have experience with it.

You need to set the response type to text/json in the API headers @steve_synopsis

More here:

Thanks for the response, but I’m still stumped. I realised as well that I had not set the URL for the API. Once I did this however, I get nothing at all for the response and the status and error are undefined.

I added the header and put it as both fields Accept and Content-Type with text/json and application json but no dice.

The updated blocks are here -

and my PHP code is

include_once('config.php');
	$sql = "SELECT * FROM roll limit 5";
	$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
		if(mysqli_num_rows($result)!=0){
			$response = array();
			$i = 0;
		
		while($r = mysqli_fetch_assoc($result)){
			$response[$i]['form'] = $r['form'];
			$response[$i]['name'] = $r['name'];
						
			$i++;
		}
		header('Content-Type: text/json');
		echo json_encode($response);
		} 	

I know that the PHP side may be out of the realm of this forum but would appreciate any assistance.

Thanks
Steve

If it worked on Postman try adding Postman’s default headers except host, user agent and connection. Replace * / * with application/json.Still, add a Content-Type header. Replace the value of Accept-Encoding each time to gzip, deflate,br

1 Like

@vishruth-ram Thanks. I tried that but no difference. I had forgotten to mention as well that the error shows as - “Failed to fetch data. Please test on a device or contact support.”.

oh - that sounds like a CORS thing. Guessing you don’t allow that on your server, right?

Try Live Test on your device @steve_synopsis

For me one time it works on Postman on one time it gives a response like you got in thunkable. It’s the problem with the API link, not the server. The same happened to Postwoman. ( It tried it with a different API )

@vishruth-ram I did what you suggested and it didn’t until it did! I then started taking bits out to see where it would fail and ended up with the Content-Type as application/json as the only header in the script and Accept application/json in Thunkable.

So thanks again!

One more thing, you set variable roll response to generate JSON from an object response, where the response is already a JSON.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.