Issue with receiving HTTP with Content-Type:application/x-www-form-urlencoded

I’ve faced a strange issue during my development related to the API. My goal was to create a web service that will receive POST requests from different sources.

First service:
It passes all the necessary data as the URL parameters. It has Content-Type : application/json and the empty body. There were no problems with it.

Second service:
Here starts an interesting part.
This service passes all the necessary parameters in the request body and has the Content-Type : application/x-www-form-urlencoded.
This request always returned 401 error – Unauthorized.
I’ve managed to figure out what exactly makes this request to crush.

The body of the request looks like:
originalsenderid=%2B614XXX11XX1&body=This%20is%20a%20test%20message&message=This%20is%20a%20test%20G3C7FWKNOD.

If the body parameter has spaces (%20 or +) I’m getting a 401 error – Unauthorized.

If the body parameter has no spaces, like body=Thisisatestmessage everything works fine. Spaces in other parameters also accepted as expected.

What is important here that this issue is reproducible only with Content-Type : application/x-www-form-urlencoded.
If I change it to Content-Type : application/json it also works fine. But I have no access to the request creation, so I can’t customize the request.

Also, I am using the Public RESTful Web Service as described here.
I tested it with the simple web service like this:

@RestResource(urlMapping='/messageService')    
global class ReceiveData
    {
        @HttpPost
        global static void doPost()
        {
            RestRequest req = RestContext.request;
            System.debug(req);
        }
    }

One more note – when I’m getting the issue with 401 error my endpoint is not even launched.

I’m not sure what additional info I should provide. I’m developing via scratch orgs so the link to my endpoint will be not valid soon.

Update:

endpoint that also reproduces an issue:

https://poormansorg-developer-edition.eu17.force.com/test/services/apexrest/myservice

Debug log looks like this:

enter image description here

Update2:

During the investigation I’ve noticed that this issue also reproducible only when you public domain has the additional pass specified. For example:

  • this one will return error:

https://poormansorg-developer-edition.eu17.force.com/test/services/apexrest/myservice

  • this one works fine:

https://poormansorg-developer-edition.eu17.force.com/services/apexrest/myservice

Maybe someone faced similar issues or can point me where my mistake is?

Answer

Attribution
Source : Link , Question Author : Andrey Shkolnikov , Answer Author : Community

Leave a Comment