Hey everyone, hope you're all doing well.


I am currently consuming a rest API. It takes in five headers and a JSON body request.

One of the headers is to define the Content-Type to JSON.

When testing on the REST API screen, it successfully returns the call with no errors (See below).


I then proceed to put this API call into a server action. I define the headers and pass in a JSON Body request (same ones as the one I tested on the test screen). The JSON request has been deserialized into the correct structure. Whilst debugging, the deserialized JSON object has all the correct values (same as the test).

However, whenever this server action is called, I get 415 Unsupported Media Type error (again the header has been defined as JSON).

If anyone could point out what I am doing wrong, that would be grateful. Please feel free to ask for more info if needed.

Many thanks in advance,

Suhail


(Edit: wrong image included)


Suhail Patel wrote:


Hey everyone, hope you're all doing well.


I am currently consuming a rest API. It takes in five headers and a JSON body request.

One of the headers is to define the Content-Type to JSON.

When testing on the REST API screen, it successfully returns the call with no errors (See below).


I then proceed to put this API call into a server action. I define the headers and pass in a JSON Body request (same ones as the one I tested on the test screen). The JSON request has been deserialized into the correct structure. Whilst debugging, the deserialized JSON object has all the correct values (same as the test).

However, whenever this server action is called, I get 415 Unsupported Media Type error (again the header has been defined as JSON).

If anyone could point out what I am doing wrong, that would be grateful. Please feel free to ask for more info if needed.

Many thanks in advance,

Suhail


(Edit: wrong image included)


Hi Suhail,

Please refer below link

https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Extensibility_and_Integration/REST_APIs/Exposed_REST_API/Supported_Media_Types_in_REST_API_Requests

Hope this will help :)

Regards,

Amit



Amit Verma wrote:

Suhail Patel wrote:


Hey everyone, hope you're all doing well.


I am currently consuming a rest API. It takes in five headers and a JSON body request.

One of the headers is to define the Content-Type to JSON.

When testing on the REST API screen, it successfully returns the call with no errors (See below).


I then proceed to put this API call into a server action. I define the headers and pass in a JSON Body request (same ones as the one I tested on the test screen). The JSON request has been deserialized into the correct structure. Whilst debugging, the deserialized JSON object has all the correct values (same as the test).

However, whenever this server action is called, I get 415 Unsupported Media Type error (again the header has been defined as JSON).

If anyone could point out what I am doing wrong, that would be grateful. Please feel free to ask for more info if needed.

Many thanks in advance,

Suhail


(Edit: wrong image included)


Hi Suhail,

Please refer below link

https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Extensibility_and_Integration/REST_APIs/Exposed_REST_API/Supported_Media_Types_in_REST_API_Requests

Hope this will help :)

Regards,

Amit



Hi Amit,


Thanks for the response.

I am sending in a JSON body request, therefore I set the content-type to JSON (see the image). This is the supported method (as confirmed by the API developers).

 As mentioned before, the call works on the test page but not on the server call (even though the values are the same).


Thanks again for the reply,

Suhail


Suhail,


Could you check your messagebody and header in the on before request event?

If you can submit that result over here, maybe we can see why it is sending 415.


Edit:

Your consumed post Rest should look like this:


Stefano Valente wrote:

Suhail,


Could you check your messagebody and header in the on before request event?

If you can submit that result over here, maybe we can see why it is sending 415.


Edit:

Your consumed post Rest should look like this:


Hi Stefano,


Thanks for your time.

I created a new on before request and set a breakpoint to see what was being sent (Correct me if this is not what you meant.)

The result was:


Also the request format is set to JSON:



Please let me know if I missed anything out or need further clarification.


Thanks again Stefano,

Suhail

Your request body is empty? 

-> {}

Stefano Valente wrote:

Your request body is empty? 

-> {}


Sorry Stefan, I sent the wrong thing. Correct one is below:


And the JSON being passed through (on UI level) is below:


Confirming that the values are being passed through


Sorry again,

Suhail

This JSON looks fine to me.


Now can you add the onafterresponse event and debug that returnmessage?

Maybe you get more information from it.

Stefano Valente wrote:

This JSON looks fine to me.


Now can you add the onafterresponse event and debug that returnmessage?

Maybe you get more information from it.

I tried that, unfortunately, it never gets hit. The all exception flow on the UI level catches the error before getting to the On After Response. Removing that gives me an internal error


This is strange since you should get the 415 in the on after response.



Stefano Valente wrote:

This is strange since you should get the 415 in the on after response.



Sorry Stefan, I forgot to publish (I need some coffee).
Here is the response:


Could you check the ... of the ResponseText?

There seems to be a lot of empty space there, but not empty.

Stefano Valente wrote:

Could you check the ... of the ResponseText?

There seems to be a lot of empty space there, but not empty.


Really appreciate all your help so far

I am not sure but i think the problem is the accept of the exposing service. I think its not set to application/JSON.


Ofcourse i am not sure but if thats not the case, i think we need help from other users over here ;)


Another thing:  I see double "" for each of your request items. But in the response i don't see them. Could this be the problem?

Stefano Valente wrote:

I am not sure but i think the problem is the accept of the exposing service. I think its not set to application/JSON.


Ofcourse i am not sure but if thats not the case, i think we need help from other users over here ;)


Another thing:  I see double "" for each of your request items. But in the response i don't see them. Could this be the problem?

Some background context, I am currently sending in a dummy JSON request to the API (sent by the API developers) to test the connectivity.

In the test REST area in OutSystems, in the request input, I copied and pasted as is the JSON payload the devs sent me:


As this is quite a large JSON payload, In the UI flow, I used the Deserialize tool to convert the JSON string into the values in objects in OutSystems. However, whenever I used the tool, it gave me an error. See below:


So what I proceeded to do was use a text editor, replace all the " with . I then also added double quotes at the start and the end of the new text (as it gave ame a syntax error).

So this is the input being fed now to the API call:


This seemed to be fine, again checking the images from the previous posts, the values seem to have deserialized fine.


I also checked in the test REST area using this new input, and it called the API fine.


But as you suggested, this may be the reason why the call is giving the error


So, with this adjustment your API now works ok?


This means we have found the problem. If you want to use the " instead of the ': this is possible but you need to check carefully. Each " must be doubled and when combining with variables you need 3 in a row. I think you missed one or two. Those are common errors.

Stefano Valente wrote:

So, with this adjustment your API now works ok?


This means we have found the problem. If you want to use the " instead of the ': this is possible but you need to check carefully. Each " must be doubled and when combining with variables you need 3 in a row. I think you missed one or two. Those are common errors.

Sorry for not making myself clear, it didn't work.

What I meant to say is the 2 different inputs both worked on the test area but not when the api was being called from a server action


Hi Stefano,

Maybe it is related to encoding, can you confirm what is the encode used on the API it is the same on OutSystems (utf-8)?

Besides that, you can verify if there is some invalid character in the JSON content.

Font: https://www.keycdn.com/support/415-unsupported-media-type

Another tip, verify in the API log what's happening on the request processing.

Solution

Hi Leandro / everyone,


Thanks for the reply.

I talked with the API devs and they advised that the reason I was getting the errors was due to the addition of the charset in the content-type header 

(pic from earlier post)

They said they'll work on the API so the call can be made without any errors.

Thank you all for your efforts. Greatly appreciated.


Thanks,

Suhail

Solution

I didnot expect this because it worked in Test. Shouldn't Test have the same headersettings?

Stefano Valente wrote:

I didnot expect this because it worked in Test. Shouldn't Test have the same headersettings?

In test, I did specify it to application/json and did the same in the server call.

I have no idea why OutSystems decided to add the charset in the server call and not in the test :s


Overwrite content-type header at OnBeforeRequest. 

It worked for me.