# Incorrect Response Data to API Call

**URL:** <https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035>\
**Category:** Web API's\
**Created:** [September 25, 2023, 5:06pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035 "2023-09-25T17:06:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve\_synopsis](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@steve\_synopsis](https://community.thunkable.com/u/steve_synopsis)\
**Post date:** [September 25, 2023, 5:06pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/1 "2023-09-25T17:06:21Z")

</div>

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 -

 ![2023-09-25 13 03 51 (2)](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/4/54eb613f7300b320ab96066c77209e655538b088.png)

But when I run the Thunkable script -

 ![2023-09-25 13 03 51](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/a/aad03f9018c39fe2c39ee27620e91910e15e9a3e.png)

I get this -

 ![2023-09-25 13 03 50](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/1/41019094cfb2c699c5006011ff21a56dd5c75307.png)

Any assistance would be appreciated.

Thanks  
Steve

---

<div class="post-metadata">

**Author:** ![steve\_synopsis](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@steve\_synopsis](https://community.thunkable.com/u/steve_synopsis)\
**Post date:** [September 26, 2023, 2:18pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/2 "2023-09-26T14:18:38Z")

</div>

Anyone ?? @tatiang @matt_conroy

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [September 26, 2023, 2:43pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/3 "2023-09-26T14:43:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [September 26, 2023, 2:56pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/4 "2023-09-26T14:56:01Z")

</div>

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

More here:

> **[Setting Headers](https://docs.thunkable.com/blocks/advanced-app-features/web-api/no-auth-apis/setting-headers)**

---

<div class="post-metadata">

**Author:** ![steve\_synopsis](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@steve\_synopsis](https://community.thunkable.com/u/steve_synopsis)\
**Post date:** [September 26, 2023, 6:34pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/5 "2023-09-26T18:34:29Z")

</div>

> [@domhnallohanlon](#):
>
> text/json

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 -

 ![2023-09-26 14 29 36](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/a/5a0ddd340c04dff22d11fe49b3394bf5041a2f77.png)

and my PHP code is

```auto
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

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 27, 2023, 11:23am UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/7 "2023-09-27T11:23:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![steve\_synopsis](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@steve\_synopsis](https://community.thunkable.com/u/steve_synopsis)\
**Post date:** [September 27, 2023, 2:30pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/8 "2023-09-27T14:30:42Z")

</div>

@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.”.

 ![2023-09-27 10 20 36](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/6/46de6454118c2bef857db365c0c5b1a9af6336cb.png)

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [September 27, 2023, 2:43pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/9 "2023-09-27T14:43:00Z")

</div>

> [@steve\_synopsis](#):
>
> 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

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 28, 2023, 3:38am UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/10 "2023-09-28T03:38:16Z")

</div>

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 )

---

<div class="post-metadata">

**Author:** ![steve\_synopsis](https://avatars.discourse-cdn.com/v4/letter/s/48db29/32.png) [@steve\_synopsis](https://community.thunkable.com/u/steve_synopsis)\
**Post date:** [September 28, 2023, 4:44pm UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/11 "2023-09-28T16:44:23Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![vishruth-ram](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vishruth-ram](https://community.thunkable.com/u/vishruth-ram)\
**Post date:** [September 29, 2023, 11:42am UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/12 "2023-09-29T11:42:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)\
**Post date:** [December 28, 2023, 11:43am UTC](https://community.thunkable.com/t/incorrect-response-data-to-api-call/2613035/13 "2023-12-28T11:43:09Z")

</div>

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