Skip to main content
Solved

Rest API with text/plain response

  • January 10, 2025
  • 4 replies
  • 88 views

Hello,


I'm working with a REST API where the response is a valid JSON but has a Content-Type: text/plain. The response looks like this:

[{"OrderId":"46D2A6BD-8B34-4F55-B816-B90017CDBA28","OrderNumber":131,"DeviceId":"POS41232","VendorId":1,"VendorName":"Test Facility","StoreId":114}]

However, the log shows the following:
2025-01-10T15:33:16.340+01:00    2    [1|Q-Id]    [HTTP|Res: 0] HTTP/1.1 200 OK, 1615 Bytes Transferred
2025-01-10T15:33:16.340+01:00    2    [1|Q-Id]    [HTTP|Res: 0] Request completed in 676 ms.
2025-01-10T15:33:16.344+01:00    3    [1|Q-Id]    [META|Schema: JSONValidation] https://pos.no/getsales is not a valid JSON resource.
2025-01-10T15:33:16.345+01:00    3    [1|Q-Id]    [META|Schema: JSONValidation] Invalid JSON markup. Expected json, but instead found [text/plain; charset=utf-8].


 

To attempt to fix this, I set Accept: application/json in the request headers, but this results in a response wrapped in a string, like this:

"[{\"OrderId\":\"46D2A6BD-8B34-4F55-B816-B90017CDBA28\",\"OrderNumber\":131,\"DeviceId\":\"POS41232\",\"VendorId\":1,\"VendorName\":\"Test Facility\",\"StoreId\":114}]"

And the log now shows:
2025-01-10T15:39:24.450+01:00    2    [1|Q-Id]    [HTTP|Res: 0] HTTP/1.1 200 OK, 8456 Bytes Transferred
2025-01-10T15:39:24.451+01:00    2    [1|Q-Id]    [HTTP|Res: 0] Request completed in 672 ms.
2025-01-10T15:39:24.456+01:00    3    [1|Q-Id]    [META|Schema: JSONValidation] https://pos.no/getsales is not a valid JSON resource.
2025-01-10T15:39:24.457+01:00    3    [1|Q-Id]    [META|Schema: JSONValidation] Length cannot be less than zero.

 

Question

It seems like the API is either returning JSON with the wrong content type (text/plain) or wrapping the JSON in a string when I request application/json.

What is the best way to handle this?

Any insights would be greatly appreciated.

Thanks!

Best answer by Thomas Lind

Hi ​@EivindJ 

Try to add the following in the Other field of the CData REST setup.

IgnoreContentType=true

I have had similar questions before and this was a solution in one of these.

View original
Did this topic help you find an answer to your question?

4 replies

Thomas Lind
Community Manager
Forum|alt.badge.img+5
  • Community Manager
  • 1020 replies
  • January 13, 2025

Hi ​@EivindJ 

When you run the call in something like Postman, do you see this text/plain header in the result and does it give the same error if you force the application/json content type?

Also, when you force a content type in a rsd file it is done like so.

<api:set attr="ContentType" value="application/json" /

The difference is that you normally would add a - so it is content-type, but that will not work for RSD files.


  • Author
  • Starter
  • 2 replies
  • January 13, 2025

Hi!

Yes, I can see the header Content-Type: text/plain in the response in Postman.
 

In Postman:
If I set the header Content-Type: application/json, the response is still text/plain.
If I set the header Accept: application/json the response is application/json but now wrapped in a string.


In TX:
I have tried to set Content-Type like this as a Custom Header in “Edit CData Data Source” (both with and without the ‘-’) but I still get the error: “Invalid JSON markup. Expected json, but instead found [text/plain; charset=utf-8].”

 

 


Thomas Lind
Community Manager
Forum|alt.badge.img+5
  • Community Manager
  • 1020 replies
  • Answer
  • January 13, 2025

Hi ​@EivindJ 

Try to add the following in the Other field of the CData REST setup.

IgnoreContentType=true

I have had similar questions before and this was a solution in one of these.


  • Author
  • Starter
  • 2 replies
  • January 13, 2025

This works! Thank you!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings